Fake "click" to activate an onclick method
I have an element with an onclick method.
I would like to activate that method (or: fake a click on this element) within another function.
Is this possible?
Once you have selected an element you can call click()
document.getElementById('link').click();
see: https://developer.mozilla.org/En/DOM/Element.click
I don't remember if this works on IE, but it should. I don't have a windows machine nearby.
If you're using JQuery you can do:
$('#elementid').click();
I could be misinterpreting your question, but, yes, this is possible. The way that I would go about doing it is this:
var oElement = document.getElementById('elementId'); // get a reference to your element
oElement.onclick = clickHandler; // assign its click function a function reference
function clickHandler() {
// this function will be called whenever the element is clicked
// and can also be called from the context of other functions
}
Now, whenever this element is clicked, the code in clickHandler
will execute. Similarly, you can execute the same code by calling the function from within the context of other functions (or even assign clickHandler
to handle events triggered by other elements)>
If you're using jQuery, you need to use the .trigger
function, so it would be something like:
element.trigger('click');
http://api.jquery.com/trigger/