4

I can programmatically go to an url, get the response and parse it. But can I trigger the javascript click event of an html element of that response? For example, let's say the response contains an element:

<div id="test">Click me</div>

And the page handles the click event like:

$("#test").on('click'..... etc.

So, is there a way that I can trigger that event after I get the html response?

Nafis Abdullah Khan
  • 2,062
  • 3
  • 22
  • 39
  • On the client, Only using a bookmarklet or user script – mplungjan Dec 06 '15 at 17:40
  • I'm a little lost on what you're trying to do – Nick Zuber Dec 06 '15 at 17:41
  • DOM elements have a [native `.click()` method](http://stackoverflow.com/questions/7999806/jquery-how-to-trigger-click-event-on-href-element/28015837#28015837). Try: `$("#test")[0].click()`. – Josh Crozier Dec 06 '15 at 17:41
  • You can't do it with client side scripting like javascript I think. You will need to use a server side script to do that with. Look into curl if you use php. – Chris Dec 06 '15 at 17:43
  • After hitting your url , use [trigger](http://api.jquery.com/trigger/) after displaying the content. Haven't tried it . Hope this works out your problem – akgaur Dec 06 '15 at 17:51
  • 1
    _"can programmatically go to an url, get the response and parse it. But can I trigger the javascript click event of an html element of that response?"_ Is ` – guest271314 Dec 06 '15 at 17:57

1 Answers1

0

DOM elements have a .click()

So, something like this works:

 <body>
    <a href="" id="myLink">Test me!</a>
</body>

And then you have this:

// basic function to be called
function clickMe() {
  alert("Clicked!");
}

$(document).ready(function() {

  // click event handler
  $("#myLink").on('click', function() {
    clickMe();
  });

  // invoke the click
  $("#myLink").click();

});

I hope this helps.

Mario Mucalo
  • 597
  • 5
  • 16