SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Onclick in onclick

    Hi,

    I ran in to some trouble a couple of hours ago and can't get it to work..

    The problem is that I have a div which has onclick and another div in that div who also has onclick. When I execute the second onclick the function is run twice (it's the same function).

    My code:

    Code:
    <div onclick="toggle();">
       <div onclick="toogle();"></div>
    </div>
    The toogle function checks if the div is visible or not and turns it and off accordingly. When the first div is on and the seconds get toggled the first one turns off.

    Hope you understand and thanks for any ideas!

  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 martinnl,

    The click event on the child DIV "bubbles" up to the parent DIV. There are several different solutions - here is one idea:

    The toggle function can stop the propagation of the event so that once the child processes the event it will not bubble up to the parent. Here is a function you can use: xStopPropagation, and here is an example of using it:
    Code:
    function toggle(evt)
    {
      // your code here
    
      xStopPropagation(evt);
    }


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
  •