Using Jquery, hide a div that is not existing yet, but will exist

Viewed 134

I want to hide a div using JQuery on click event of another div. The problem is that the div that I want to hide is actually created only when I click on the existing div.

Let me explain with code example:

HTML:

<div class="first">I already exist and I create the second div when I am clicked</div>
<div class="second">I do not exist yet, I am created when first div is clicked</div>

The above is happening by another piece of code that another developer wrote.

I want to write another piece of code that will hide the second div when its created right after first div is clicked.

I do not have the permission to change the original dev's original code.

My JQuery that is not working:

$(".first").on("click",function() {
    $(".second").hide();
});
1 Answers

You should use this, cause you have to make this delegate. explanation: https://api.jquery.com/on/

$("body").on("click", ".first",function() {
    $(".second").hide();
});
Related