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,438
  • 68
  • 55
  • 126
subir biswas
  • 349
  • 1
  • 4
  • 13

3 Answers3

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,326
  • 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,288
  • 1
  • 16
  • 26
0

You can use

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

in vanilla JS

Lapskaus
  • 1,578
  • 1
  • 10
  • 21