I have a bunch of <a>
tags on a page that look something like
<a href="#" id="001" onclick="fnaaa();" >...</a>
...
<a href="#" id="002" onclick="fnaba();" >...</a>
...
<a href="#" id="003" onclick="fncda();" >...</a>
//sometimes maybe like this
<a href="#" id="004" onclick="fnagg(); return false;" >...</a>
...
Now I have the id passed to the page as a query string , so I originally wanted to do something like
$('a[id="' + id + '"]').click();
$('a[id="' + id + '"]').trigger("click");
it turns out both of those are not allowed , so if I have the id , how can I call the function that is written in the onclick attribute? I know I can probably get it like this
var funcToCall = $('a[id="' + id + '"]').attr('onclick');
but how do I call this funcToCall? remembering that funcToCall may be more then just a function name ex. "fnagg(); return false;"
Attaching the event dynamically className = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!
The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.
You are using onclick
attribute to bind the handler. Try like below,
document.getElementById(id).click();
DEMO: http://jsfiddle.net/kpvKG/
First of all, the ID attribute has some restrictions, one being that it must start with a letter. After you fix that, I would recommend not using an inline onclick
handler.
$("#ID_HERE").click(function(e) {
fnaaa();
e.preventDefault();
});
Then you can trigger it easily:
$("#ID_HERE").triggerHandler("click");
However, if you absolutely must use the ugly onclick, you can invoke it like this:
<a id="foo" href="#" onclick="alert('test');">Test</a>
var el = document.getElementById('foo');
el.onclick();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With