I'm loading an element on a page with ajax and jQuery for a personal project. But it's impossible to display some element in a very specific situation after they're append to the DOM.
Some html loads with ajax need to be displayed when the user click on another element loaded with ajax.
Example:
<!-- Element load with ajax and append to DOM -->
<div class="block1">
<p>You have clicked</p>
</div>
<div class="block2">
<p>Some Text</p>
</div>
I have .block1
which is set to display: none;
in CSS, and I want that after a click on .block2
the CSS becomes display: flex
for .block1
.
So I have made this function with jQuery:
$(".block2").click(function () {
if ($(".block1").css("display") === "none") {
$(".block1").css("display", "flex");
} else {
$(".block1").css("display", "none");
}
});
My trouble is that:
The function work fine when
.block1
is set todisplay: flex
by default (one click on.block2
makes disappear.block1
, the next click makes appear.block1
).This doesn't work when
.block1
is set by default ondisplay: none
. But the function is correctly detect (I had set someconsole.log()
message, and the CSS are updated too).
For information:
- The CSS are initialized in a stylesheet.
So my question is:
Why this isn't working when the CSS is by default display: none;
, and work perfectly when the CSS is by default display: flex;
. And how I can fix that?