Jump to content

Simulating href click


cesarcesar

Recommended Posts

i need to simulated having clicked on a link with JavaScript. As you will see in my stripped down example the resulting function from the click is a window.addEvent() action. Thanks for the help.

 

js code -

window.addEvent('domready', function() {
     expandCollapse();
}

function expandCollapse(){
     $('expand').addEvent('click', function(){
          // do something
     }
}

 

html code -

<a id="expand" href="javascript:void(0);">open all</a>

 

i have tried this but id didnt work -

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

 

What is the code to make the window.addEvent() fire? *expand* must be part of it. Thank you.

Link to comment
Share on other sites

Thanks to Fang -

 

Working Solution -

function fireOnclick(objID) {
var target=document.getElementById(objID);
if(document.dispatchEvent) { // W3C
    var oEvent = document.createEvent( "MouseEvents" );
    oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    target.dispatchEvent( oEvent );
    }
else if(document.fireEvent) { // IE
    target.fireEvent("onclick");
    }    
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.