1

How do I make a web browser fire a click to an #id element on the page?

Using the .click in javascript, doesn't work, as although .click is being called within a function, that function however is not executed upon a click (otherwise it would work).

Please help.

$('#gocircus').live('click', function() {
document.getElementById('something').click();
});

Clickrobot

desbest
  • 4,746
  • 11
  • 51
  • 84

3 Answers3

2
var el = document.getElementById('id');
el.onclick();

assuming you are assigning function to that element onclick previously

el.onclick = function() { // your code }

Your code doesn't work as click() is jQuery extension of click event, you can't use it on native DOMElement, instead do:

$('#gocircus').live('click', function() {
   $('#something').click();
}).click();

As per my comment, live is deprecated as of 1.7, use on():

$(document).on('click', '#gocircus', function() {
   $('#something').click();
}).click();

EDIT

As per your case, I think you need to bind click event to GO! instead:

$('#go').click(function() {
   for(var i = 0; i < $('#manyTimesToClick').val(); i++) {
      $('#idToClick').click();
   }
});
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • It doesn't work. I want the element itself to be clicked (firing a click/simulate a click), not something to happen once it has been clicked. – desbest Apr 25 '12 at 03:16
  • It doesn't work. My bookmarklet is here, and it only works in Safari for now. http://desbest.uk.to/clickrobot/clickrobot.js Also... use the bookmarklet below. – desbest Apr 25 '12 at 03:18
  • @desbest ... Getting confused on what you are trying to do, I see the image you added on my answer, so after I fill in the element id to click, I should fire a click event on that id ? – Andreas Wong Apr 25 '12 at 03:22
  • It's a bookmarklet that is designed to automate clicks. I type in the #id of the element I want to click, how many times I want it clicked, and the delay to wait before subsequent clicks. It would be useful for websites with no pagination, and infinite scrolling, that requires users to click the "more" button, to see more content. Then someone get the computer to click "more" 15 times, automated. – desbest Apr 25 '12 at 03:24
  • 1
    @desbest Then you need to bind your click event on `GO` button, I'll add an example code to my answer – Andreas Wong Apr 25 '12 at 03:25
  • Hmm it's nearly finished. It seems like the `$('#idToClick').click();` works for clicking elements that I've created in my bookmarklet, but that it doesn't have the ability to click elements on the webpage I've loaded up the bookmarklet on. – desbest Apr 25 '12 at 03:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10454/discussion-between-niftydude-and-desbest) – Andreas Wong Apr 25 '12 at 03:59
  • 1
    **I managed to make my Clickrobot bookmarklet work!** I've found out that for security reasons, web browsers only execute the .click action on certain elements, if the web browser is sure that that action is performed due to an onClick event. I then updated the code accordingly, and not it works. Thanks a lot for your help! – desbest Apr 25 '12 at 13:49
1

if you are open to jquery you may try using the trigger event to tiger the button http://api.jquery.com/trigger/

$("#gocircus").click(function () {
$("something").trigger('click');


});
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • I can get the element to fade out, but not to click it. This has to be a WebKit Safari issue, as Webkit. – desbest Apr 25 '12 at 12:16
  • I can get the element to fade out, but not get the browser to click it. This must be a Webkit/Safari issue, as Webkit has ways of sabotaging the way javascript should work, for "security reasons". -_- – desbest Apr 25 '12 at 12:17
  • **I managed to make my Clickrobot bookmarklet work!** I've found out that for security reasons, web browsers only execute the .click action on certain elements, if the web browser is sure that that action is performed due to an onClick event. I then updated the code accordingly, and not it works. Thanks a lot for your help! – desbest Apr 25 '12 at 13:56
0

The method you need are dispatchEvent or fireEvent, depending on the browser.

Here is an example code snippet

function eventFire(el, etype){
  if (el.fireEvent) {
    (el.fireEvent('on' + etype));
  } else {
    var evObj = document.createEvent('Events');
    evObj.initEvent(etype, true, false);
    el.dispatchEvent(evObj);
  }
}

From here: How to simulate a click with JavaScript?

Community
  • 1
  • 1
DG.
  • 3,417
  • 2
  • 23
  • 28
  • I tried that, and I couldn't figure out how to make it work. Do I use `eventFire(elementId, click);`? – desbest Apr 25 '12 at 03:54
  • I have not ever used it, but I think it should be like `el=document.getElementById(elementId); eventFire(el, 'click')` – DG. Apr 25 '12 at 04:01
  • Try 'onclick'. If that doesn't work, it becomes time to start thinking for yourself, I guess. I've given you enough clues that you should be able to complete the task with a little bit of research and testing. – DG. Apr 25 '12 at 07:53
  • 1
    To future readers of this post. MY ANSWER IS CORRECT. The OP voted me down because he doesn't like may attitude, I suppose, which is fine, because I don't like his. – DG. Apr 27 '12 at 23:54
  • I do like your attitude. I just personally cannot figure out how to make the code work. – desbest Apr 28 '12 at 10:08
  • Did you mark my answer -1? Being unable to "personally... figure out how to make the code work" is not a good reason to mark an answer -1. – DG. May 09 '12 at 20:57