A problem with childs and parents ...

Discussion in 'JavaScript' started by almasry, Jun 17, 2008.

  1. #1
    Hi guys ,

    I am stuck with a little problem with JQuery , I want to apply an OnClick event to a parent div and all of its childs except one child, the child has its own Onclick event .

    What happens is that when I click on that specific child , it executes its OnClick event and then executes the parent's event.

    The problem here is that I want the specific child to execute its event only without executing the parent event ...

    that's my code :

    $("#parent").click(function()
    {
       // executes the parent's event ......
       // This applies to all childs   ......
     });	 
    
    $("#specific-child").click(function()
    {
       // executes the child's event ......
     });	 
    HTML:

    And this is the HTML structure :

    
    <div id="parent">
    
       <div> text ....</div>
    
       <div> text ....</div>
    
       <div> text ....</div>
    
       <div> text ....</div>
    
       <div id="specific-child"> specific content over here ...</div>
    
    </div>
    HTML:
     
    almasry, Jun 17, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hmm... I think the easiest way would be to do something like this:

    $("#parent").click(function(e){
    	e = e || window.event;
    	if ((e.srcElement || e.target).id == 'specific-child')
    		return; //leave the function
    ...
    PHP:
     
    MMJ, Jun 17, 2008 IP
  3. almasry

    almasry Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks so much for your help , but actually it doesn't work .

    I could figure out something like :
     if ($(e.target).attr('id') == 'specific-child') return;
    PHP:
    that's working for Jquery using the factory function ($)and it worked perfectly .

    thanks one more time .
     
    almasry, Jun 17, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Strange, I tried it.

    Glad you got it to work though.

    You might want to post on the sitepoint forum that you got it solved.
     
    MMJ, Jun 17, 2008 IP