Skip to main content

Javascript programmatically activate an onclick method

If you're using JQuery you can do:

$('#elementid').click();
------

Once you have selected an element you can call click()

document.getElementById('link').click();

see: https://developer.mozilla.org/En/DOM/Element.click

------

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
}

 -

------

<div id="c" onclick="alert('hello')">Click me!</div>

<div onclick="document.getElementById('c').onclick()">Fake click the previous link!</div>

------------

val(document.getElementById('elementId').getAttribute('onclick'));

Reference:

 http://stackoverflow.com/questions/347512/fake-click-to-activate-an-onclick-method