0

I have an anchor tag with an onlick attribute containing some javascript code. I need to get this javascript executed using jquery or javascript. I have tried to call the click event on the anchor tag element but nothing happens. Clicking the anchor tag manually on the site works just fine but I cannot get it to work thru jquery.

/Måns

1 Answers1

4

Without example code I don't see any reason why something like this wouldn't work:

$('#some-link').click();

A possibly unsafe alternative would be...

var oc = $('#some-link').attr('onclick');
eval(oc);
ryan
  • 6,541
  • 5
  • 43
  • 68
  • 1
    Note: indirect calls to `eval` use the global scope; to hide your current scope from the `onclick` code, do `e=eval; e(oc)` or `(new Function(oc))()`; more importantly, note that this only executes the `onclick` attribute, and `onclick` shouldn't be used with jQuery. – John Dvorak Mar 17 '13 at 16:35
  • This jsfiddle should help demonstrate how to proceed. http://jsfiddle.net/VXEdb/1/ – KryptoniteDove Mar 17 '13 at 17:01
  • Well, I've tried that but it does not work. The javascript code set on the onlick attribute wont execute. – Måns Tånneryd Mar 17 '13 at 18:24
  • I finally figured it out. The full story is that I am using awesomium to load and manipulate websites and I could not get the jquery javascripts executed by awesomium to work with the click event on the anchor tag in question. But after many long hours I finally noticed that the page apparently was not fully loaded even though it seemed to be. Waiting for it to load completely solved my problem. It works fine now! Thanks to all of you for taking the time to answer my question. – Måns Tånneryd Mar 17 '13 at 18:50
  • It is a little bit strange though. The DOM was ready when I tried to use jquery to click the anchor tag so why it did not work unless I awaited the load completion I'm not quite sure. – Måns Tånneryd Mar 17 '13 at 18:54