How to use simulate the action of clicking button with jQuery or JavaScript?
I need to use JavaScript to simulate the action of clicking buttons. How do I achieve it? Can I achieve it with jQuery?
Yes, you can do it with jquery. Use trigger
function. Here documentation. Here is sample:
$('#foo').trigger('click');
You can execute the click event handler assigned to a button control:
$("#mybutton").click();
Simply .click():
$("#theButton").click();
Following code will print clicked two times one from $('.submitBtn').click();
and other $('.submitBtn').trigger('click')
$(document).ready(function() {
$('.submitBtn').on('click', function() {
console.log("Clicked!");
})
$('.submitBtn').click();
$('.submitBtn').trigger('click')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button menu="submitBtn" class="submitBtn len4 btn" title="submit" data-code-m="sbm.submit"> Click me
</button>