Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery select change not firing

I need to capture when a select box changes, should be simple!

    $('#multiid').change(function(){     alert('Change Happened'); }); 

But it does not work, I suspected the problem is that the select box does not exist on document ready it is created only if needed, so I created it empty in HTML, populated it with code as a test but that didn't work either.

function buildmulti(id,name,price) {     // build action items for action bar     var optlist = $('<select></select>').attr('id', 'multiid').attr('name', 'multiid');     optlist.append('<option value="0">Select Size</option>');     $.each(option, function(index, val) {         if(val.prodID *1  == id * 1) {             v = val.ID;             fprice = price * 1 + val.pricechange * 1;             t = name + ' - ' + val.variation +  ' - ' + currency + (fprice).toFixed(2);             optlist.append('<option value="' + v + '">' + t + '</option>');         }     })     $('#addbasket').append(optlist); }; 

it's probably another bracket out of place, but I can't find it!

like image 394
Pete Ravenscroft Avatar asked Oct 05 '13 05:10

Pete Ravenscroft


People also ask

Why is Onchange not working?

onchange is not fired when the value of an input is changed. It is only changed when the input's value is changed and then the input is blurred. What you'll need to do is capture the keypress event when fired in the given input. Then you'll test the value of the input against the value before it was keypressed.

Does jQuery Val trigger change event?

When you dynamically set a value in a textfield using jQuery . val(), you have to manually trigger the . change event if you want to add extra code that trigger when the value of the field change.

How do I set the value of a element in jQuery?

The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

Why this is used in jQuery?

The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.


1 Answers

Try

 $(document).on('change','#multiid',function(){     alert('Change Happened'); }); 

As your select-box is generated from the code, so you have to use event delegation, where in place of $(document) you can have closest parent element.

Or

$(document.body).on('change','#multiid',function(){     alert('Change Happened'); }); 

Update:

Second one works fine, there is another change of selector to make it work.

$('#addbasket').on('change','#multiid',function(){     alert('Change Happened'); }); 

Ideally we should use $("#addbasket") as it's the closest parent element [As i have mentioned above].

like image 66
Dhaval Marthak Avatar answered Oct 20 '22 03:10

Dhaval Marthak