AwesomeDude3247
AwesomeDude3247

Reputation: 98

How would I use the `click` function on a `div` element?

I tried to do

var js = document.getElementById('product16');
js.click

But that only works for buttons? Any help is much appreciated

Upvotes: 0

Views: 853

Answers (2)

Umair Ahmed
Umair Ahmed

Reputation: 562

you need to call the click function to simulate a click on an element so it would be like this


var js = document.getElementById('product16');
js.click() // you need to call the click handler 

JS Fiddle: https://jsfiddle.net/ae1yujkc/1/

Upvotes: 0

David I. Samudio
David I. Samudio

Reputation: 2693

TL;DR: Instead of referencing the click function (js.click), try calling it: js.click().

NL;PR: As Seblor mentioned, any HTML element allows event listeners binding.

This pastebin shows an alert triggered programmatically from a function bound to a div's click event.

var js = document.getElementById('product16');
js.click = function (){alert("div click");}; // assign some behavior
js.click(); // trigger it
<div id="product16">
hello
</div>

Upvotes: 1

Related Questions