www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Simulating href click

  1. #1
    Join Date
    Nov 2006
    Posts
    70

    resolved [RESOLVED] 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.

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    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");
        }    
    }

  3. #3
    Join Date
    Nov 2006
    Posts
    70
    works perfectly. thanks soooo much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles