Thomas Russell
Thomas Russell

Reputation: 5970

jQuery .val() not returning what is expected

I am trying to implement the following code snippet into my project: http://jsfiddle.net/Zmf6t/. But I am running into a bit of difficulty; I have wrapped the JavaScript in the jQuery(document).ready() callback function, and checking it in the Chrome debugger shows that the script is running. However, when I get to the if( jQuery(this).val() == "-1" ) (as my <select> value has -1 as its value), the debugger reports that the elements value property is "-1", but the statement within the if is never evaluated. Instead the else property is evaluated.

Does anyone have any idea why this might be happening? I cannot seem to isolate the issue to provide a minimal working example; my jQuery is as below:

jQuery(document).ready( function() {    
    jQuery('#test').change( function() {
        if( jQuery(this).val() == "-1" )
        {
            jQuery(this).addClass( "empty-combo" );
        }
        else
        {
            jQuery(this).removeClass( "empty-combo" );
        }
    });

    jQuery('#test').change();
});

Upvotes: 1

Views: 76

Answers (1)

Legendary
Legendary

Reputation: 2242

Change your condition to:

if( jQuery(this).val() == null )

It worked.

Upvotes: 2

Related Questions