theorise
theorise

Reputation: 7445

jQuery select value not set

I am trying to set an action for when a select value is between 0 and 2, but with the function below, I am getting the action triggering when the value is not defined. What is the best method for doing so?

$('#html_select').click(function(){   
    var option = $(this).val();   
    if(option < 3 && option != null){  
    //Do something
   } 
});

Upvotes: 0

Views: 739

Answers (1)

Naftali
Naftali

Reputation: 146360

What you are doing is perfectly fine.

It is usually better to use the onchange method:

$('#html_select').change(function(){   
    var option = this.value; //DOM's value   
    if(option < 3 && option != null){  
    //Do something
   } 
});

Fiddle: http://jsfiddle.net/maniator/pFFAH/

Upvotes: 2

Related Questions