March 22, 2013

Writing jQuery code

Question by Roman

What i am doing wrong. I want to write in one field and after that opened by the following.I want thet if i enter the text in one field and after that other field are opening but other field dus not open’s. Please help me

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script>

    $(document).ready(function() {
        $(function(){
            if($('#name' == '')){
                $('#version').attr( 'disabled', 'disabled' );
            }
            else {
                $('#version').removeAttr( 'disabled', 'disabled');  
            }
        }); 

        $(function(){
            if($('#name' !== '')){
                $('#version').attr( 'disabled', 'disabled' );
            }
            else {
                $('#version').removeAttr( 'disabled', 'disabled');  
            }
        });
    });
</script>
</head>
<body>
    <div>
        <form>
        Name: <input type="text" name="name" id="name">
        Version: <input type="text" name="version" id="version">
        Build: <input type="text" name="build" id="build">
        <input type="submit" value="Submit" id="submit"></br>
        <input type="text">
        </form>
    </div>
</body>
</html>

Answer by Starx

This is invalid:

if($('#name' == '')){
    $('#version').attr( 'disabled', 'disabled' );
}

You can use this instead:

if($('#name' ).length){
    $('#version').attr( 'disabled', 'disabled' );
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!