2

I have just started learning HTML, CSS and jQuery and I am new. I searched for binding event handlers to dynamically added elements and I found out I have to use the parent of the dynamic element as the static selector and add the dynamic element after the event method ( Correct me if I am wrong).
$(staticAncestors).on(eventName, dynamicChild, function() {});
But I do not know why the code below does not work when I change :

  1. $("#p1") with $(this)
    or
  2. $(document) with $("div")

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $("div").click(function(){
        $(this).html('<p id="p1" style="background: red;">Paragraph one</p> <p>Paragraph two</p>').css('margin', '15px 15px 15px 15px');
    });
    $(document).on('click', '#p1', function(){
        $("#p1").css('background-color', 'blue');
    });
});

</script>
</head>
<body>
<div> It's a div. </div>
</body>
</html>

1
  • Very strange. Adding console.log(this.id); shows that the handler is running. But for some reason the CSS isn't changing. Commented Oct 20, 2020 at 20:17

1 Answer 1

3

According to Jquery Docs https://api.jquery.com/on/

By default, most events bubble up from the original event target to the document element (In your case from #p1). 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().

Here is your working code.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
    $("div").click(function(){
        $(this)
        .html('<p id="p1" style="background: red;">Paragraph one</p> <p>Paragraph two</p>')
        .css('margin', '15px 15px 15px 15px');        
        
    });

    $("div").on('click', 'p', function(event){
        event.stopPropagation();
        $(this).css('background-color', 'blue');
    });
});

</script>
</head>
<body>
<div> It's a div. </div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

4 Comments

Ahh, the problem is that the other event handler is running and replacing the html again, wiping out the CSS change.
Yes, if you print following in console console.log($(this)); you'll get Node but when you click on that node you will get a info message like this "Node cannot be found in the current page."
So you mean that the first .click function overrides the .on(click) function? If you mean that this is the problem, why using $(document) and $("#p1") works but changing any of them stops the code snippet? Even if you only change $("#p1") with $(this) it prevents the code from running.
@someone $(document) works because document is the parent of div. When you use div it is the same element that has already click listener attached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.