0

I would like to write a code to trigger an event in pure javascript without clicking the button.

i would like the solution to be more generic so that it can be apllied to any event. Like if i want to trigger either button click or drop down etc without actual mouse or keyboard activity.

we have seething for submit button without clicking on it--document.formid.submit();

3

1 Answer 1

3

jQuery's trigger method can be used to trigger an event:

https://jsfiddle.net/gRoberts/4xah6nph/

$('#test').click(function(e) {
    alert('jQuery: You clicked me!');
});

setTimeout(function() {
    $('#test').trigger('click');
}, 5000);

or alternatively you can use

var elem = document.getElementById('test');
elem.addEventListener('click', function(e) {
    alert('Navtive: You clicked me!');
});
setTimeout(function() {
    elem.dispatchEvent(new Event('click'));
}, 6000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.