0

can anyone explain why I cannot change the background of an appended element? Thank you so much. (I could not submit the question without writing more text so I am writing more text.)

<!DOCTYPE html>
<head>
    <title>TEST</title>
    <style>
    div {
        background:yellow;
        width:100px;
        height:25px;
        border:1px solid black;
        margin-bottom:10px;
        }
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    $(document).ready(function(){
        $("div").on("click", function(){
            $(this).css({"background":"red"});
        });
        $("button").on("click", function(){
            $("<div></div>").appendTo("body");
        });
    });
    </script>
</head>
<body>
    <button>add div</button>
    <div></div>
</body>

Kobbe
  • 316
  • 1
  • 13

0 Answers0