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!
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.
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.
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.
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.
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].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With