SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Eventlistener for mouseclick

    Say I have three buttons and when the user clicks on one of the buttons, something happens. However, when he or she clicks anywhere else on the page something else should happen. In this case a click opens a dropdown menu, a click anywhere else on the page closes the menu (+ does whatever else the click was supposed to do)

    What is the best way to do this? I've tried setting up an eventlistener on the whole page but I can't make it happen for some reason.




    Thanks

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi A-OK,

    Code:
    document.getElementById('button1').onclick = function(evt)
    {
      // do something
    
      document.onclick = docOnclick;
    
      if (evt && evt.stopPropagation) evt.stopPropagation();
      else if (window.event) window.event.cancelBubble = true;  
    }
    
    function docOnclick(e)
    {
      // do something
    
      document.onclick = null;
    }
    There are other things to consider but without seeing your page I'm not sure what to suggest.


Bookmarks

Posting Permissions

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