Programming Tips - jQuery: simulate a user changing their choice for a select

Date: 2010jul7 Updated: 2020may6 Library: jQuery Language: javaScript Keywords: fire Q. jQuery: simulate a user changing their choice for a <select> A. This is only useful if you have a function to that reacts.
// make your function mySelectChanged() execute when the user // changes the <select> $('.myselect').change(mySelectChanged); // Later in the code, whenever you want it do this // to simulate the user making the change. Your function will execute. $('.myselect').trigger('change');
This works for other types of input too. Like <input type=checkbox> etc
// onMyClick() will be called with user clicks. // Simulate a click right away. $('#mybutton').click(onMyClick).trigger('click');