0

I want to fire an trigger click event with JavaScript. I did it with jQuery like this:

$('button[type="submit"]').trigger('click'):

I want the same with JavaScript.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
subir biswas
  • 371
  • 2
  • 5
  • 14

3 Answers3

2

You can use

document.getElementById('buttonID').click();

in vanilla JS

Lapskaus
  • 1,709
  • 1
  • 11
  • 22
1

function callClick() {
  document.querySelector('button[type="submit"]').click();
}
<button type="submit" onmouseover="callClick();" onclick="alert('Click called!')">Submit</button>

This should do the trick:

document.querySelector('button[type="submit"]').click();
dunli
  • 1,356
  • 9
  • 18
0

you can use click . The HTMLElement.click() method simulates a mouse click on an element.When click() is used with supported elements (such as an <input>), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

Syntax

setTimeout(function(){
  let submit =document.getElementById('submit');
  submit.click();
},2000)

function submited(){
  alert('submitted');
}
<form onsubmit="submited()">
 <input type="text">
 <input type="submit" id="submit">
</form>
manikant gautam
  • 3,521
  • 1
  • 17
  • 27