Current location: Hot Scripts Forums » General Web Coding » JavaScript » Simulating href click


Simulating href click

Reply
  #1 (permalink)  
Old 09-16-08, 07:06 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
Simulating href click

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 -
Code:
window.addEvent('domready', function() {
     expandCollapse();
}

function expandCollapse(){
     $('expand').addEvent('click', function(){
          // do something
     }
}
html code -
Code:
<a id="expand" href="javascript:void(0);">open all</a>
i have tried this but id didnt work -
Code:
document.getElementById('expand').click();
What is the code to make the window.addEvent() fire? *expand* must be part of it. Thank you.
Reply With Quote
  #2 (permalink)  
Old 09-16-08, 08:11 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
Thanks to Fang -

Working Solution -
Code:
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");
    }    
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Specify which xml file to load when click a link s1yfa JavaScript 3 06-13-08 06:30 PM
Click event in Crystal Report daivagna ASP.NET 1 04-17-07 08:08 AM
One click multimedia downloads? LittleWildheart JavaScript 2 10-11-04 12:10 PM
Single click refreshing with target of _top Deezul ASP 0 09-22-04 06:57 PM


All times are GMT -5. The time now is 11:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.