Show DIV when I click the Button via Jquery

Asked

Viewed 52 times

2

I’m using the following code to show the DIV when I click the button:

$(".button").click(function(){
  $(".div").css("display","flex");
});

It is working normally when I click the button created directly in html. It turns out that when I create the same button with jquery it doesn’t work.

var divs = '<div class="col-2"><button data-action="addCart" class="addCart" type="button" name="button">+</button></div>';

        $(".container").append(divs);

Both the button I was creating directly in html and the one generated via jquery (pq are multiple Divs) are exactly the same in Chrome inspection, but the second one does not change the CSS to flex. I’m using the same class for both.

  • I don’t know for sure, but maybe it doesn’t work because it was created after the DOM was rendered... I don’t know if it solves, but you can put the script at the end of the document after all the content and test to see if it solves

1 answer

2


Browser other questions tagged

You are not signed in. Login or sign up in order to post.