1

I first used append to add a new element using jQuery as such:

var $item = $("<div>")
              .addClass("box");
$("#gameDiv").append($item);

I then want to be able to mouseover this div by accessing its class:

$(document).ready(function(){
  $(".box").mouseover(function(){
    alert("test");
  });
});

However, it doesn't seem to work. Is there any reason why I cannot access this element? And how do I rectify it?

RawMeat
  • 23
  • 2
  • $(document).ready() is eecuted when DOM is loaded, so, items added after are not affected because they weren't exist yet. Attach the listener on div creation. – Triby Mar 01 '20 at 01:18

0 Answers0