Take the 2-minute tour ×

I have a div display some titles of music which is clickable. When you click it it will show some more detail information. Then I also have a button in the clickable div. When I click the button. It won't call the function of the button but the function of the div? Is there a way to solve this? Thank you!

$("#myComList").append("<div id="+comListID+" class=listDiv> <p class=comTitle><? echo $row["compositionTitle"] ?>(<?echo $row["year"]?>)</p><button id="+comListID+"btn class=addNewArrBtn>Add new Arrangement</button> <p class=comOri>BY <? echo $row["OrigComposer"] ?></p> </div>");
        $('#'+comListID).click(function() {
              $(this).find("li").slideToggle('slow', function() {
share|improve this question
Yes, it is possible. –  Pranav 웃 Dec 13 '12 at 21:46
What have you tried? Please post your code. –  James Montagne Dec 13 '12 at 21:47
I add my code for this part –  lancellx Dec 13 '12 at 21:48

3 Answers 3

up vote 5 down vote accepted

It's called 'bubbling'. The button is inside the div so it's executing button then up the chain to div. Add event.stopPropagation() inside the button function.

share|improve this answer
The method is spelled "stopPropagation". –  radbyx Nov 28 '13 at 8:00

From jQuery documentation:

By default, most events bubble up from the original event target to the document element. At each element along the way, jQuery calls any matching event handlers that have been attached. A handler can prevent the event from bubbling further up the document tree (and thus prevent handlers on those elements from running) by calling event.stopPropagation(). Any other handlers attached on the current element will run however. To prevent that, call event.stopImmediatePropagation(). (Event handlers bound to an element are called in the same order that they were bound.)


So you'd call event.stopPropagation() inside the button click handler, as to stop the div event from firing.

share|improve this answer

I believe I understand your question without seeing the code. The problem it sounds like stems from the click event bubbling or propagating up. Below is a sample of code to try and a link to a fiddle for you to test:

<div id="testDiv" onclick="alert('Stop Clicking Me!');">
    <button type="button" onclick="function(e) { alert('You hit the button!'); e.stopPropagation(); }">Click Me!</button>

In this function, the:


prevents the click event from filtering up to its parent container (in this case "testDiv") and triggering its click event as well. You can test it and see for yourself in the jsfiddle below:

Test Fiddle


In your case:


add the event parameter to the click function and stop it from propagating to the parent.

Hope this helps.

share|improve this answer

Your Answer


