3

is it possible to make a javascript to "click" a specific element?

on my page I've got a with a specific id, and I want to make a JS script to click the div on certain event. is there any method like:

document.getElementById("idofdiv").click()?
1
  • are you trying to trigger a click event? If yes, what you posted in the question itself would work :) Commented Sep 8, 2013 at 8:27

3 Answers 3

2

Yes, it is possible to trigger a click event by javascript.

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

Or with jQuery:

$('#element').click();

Please note that the click event won't work if you are trying to force a click on an anchor tag which opens a page. There are tricks called as "clickjacking" which you can use to force it, but this way you're cheating the user.

Sign up to request clarification or add additional context in comments.

Comments

0

you can do:

document.getElementById("idofdiv").onclick = function() {
  //your code here
}

or

document.getElementById("idofdiv").onclick = someFunction;
function someFunction() {
  //your code here
}

See:: onclick

Comments

0

http://www.w3schools.com/jsref/met_html_click.asp

I guess you answered your own question.

Comments

Your Answer

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