Skip to content Skip to sidebar Skip to footer

How To Get Option Value On Change And On Page Load?

I know that it is possible to do in two functions, but I am not sure how to make it in one. $('#purchase_sub_type').change(function() { if($(this).val() == 15) { console.

Solution 1:

Trigger the change event so the code fires

$("#purchase_sub_type").change(function() {
   if($(this).val() == 15) {
       console.log('is 15');
   }
   else {
      console.log('not 15');
   }
}).change();  //or .trigger("change");

Solution 2:

Generally it is good idea to give function a name.

var field = $("#purchase_sub_type");

functionlog() {
    if(field.val() == 15) {
       console.log('is 15');
    }
    else {
       console.log('not 15');
    }
};

field.change(log);
log();

Post a Comment for "How To Get Option Value On Change And On Page Load?"