Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically click on a non-button element using javascript?

Tags:

javascript

How to programmatically click on a non-button element using javascript? Or is it atleast possible in browsers like Firefox and Chrome?

like image 950
Kevin Avatar asked Jan 01 '14 17:01

Kevin


People also ask

Can JavaScript click a button programmatically?

To click a button programmatically with JavaScript, we can call the click method. const userImage = document. getElementById("imageOtherUser"); const hangoutButton = document. getElementById("hangoutButtonId"); userImage.

How do you use the click method in JavaScript?

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.

What is the difference between click and onclick in JavaScript?

click is a function on HTML elements you can call to trigger their click handlers: element. click(); onclick is a property that reflects the onclick attribute and allows you to attach a "DOM0" handler to the element for when clicks occur: element.

Can you give a div onclick?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.


2 Answers

Believe it or not, for a fairly basic click, you can just call click on it (but more below): Live Example | Live Source

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Artificial Click</title>
</head>
<body>
  <div id="foo">xxxxxxx</div>
  <script>
    (function() {
      var foo = document.getElementById("foo");
      foo.addEventListener("click", function() {
        display("Clicked");
      }, false);
      setTimeout(function() {
        display("Artificial click:");
        foo.click(); // <==================== The artificial click
      }, 500);
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = String(msg);
        document.body.appendChild(p);
      }
    })();
  </script>
</body>
</html>

You can also create the relevant type of Event object and use dispatchEvent to send it to the element:

var e = new MouseEvent("click", {
  view: window,
  bubbles: true,
  cancelable: true
});
foo.dispatchEvent(e);

This gives you the opportunity to do things like set other information (the typical pageX and pageY, for instance) on the event you're simulating.

More about the various event object types in Section 5 of the DOM Events spec.

like image 118
T.J. Crowder Avatar answered Sep 19 '22 23:09

T.J. Crowder


You can use HTMLElementObject.click()

Something like document.getElementById('div1').click()

See more

like image 37
laaposto Avatar answered Sep 17 '22 23:09

laaposto