For ex. we have
<select id="menu">
<option value="1">sample 1</option>
<option value="2">sample 2</option>
</select>
and
<input type="radio" id="parentcheck" value="1">Button 1
<input type="radio" id="parentcheck" value="2">Button 2
I want to simply "clone" user input. Let's say we want to set them to exact values and trigger their functions.
In my case i have functions for both of them
$("#menu").change(function () {...
$(".parentcheck").click(function () {...
For ex. how to select Sample 1 and Button 1 and fire their function?
For the first case you can use .val()
and trigger the event manually in both cases.
Consider the following
$('#menu').val(2).trigger('change');
In the above, we select the option with the value of 2.
And for the radio buttons
$('#parentcheck').prop('checked', true).trigger('change');
Just set the attribute of the option/checkbox
For the options it is "selected" and for the checkboxes it is "checked"
Also, trigger the event after that using trigger()
Example:
$( '#menu option[value=whatever]' ).attr( 'selected', 'selected' ).trigger( 'change' );
$( '.parentcheck' ).attr( 'checked', 'checked' ).trigger( 'click' );
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