I'm loading element on a page with ajax and JQuery for a personnal project. But it's impossible to display some element in a very specific situation after they're append to the DOM .
Some html load with ajax need to be display when the user click on another element load with ajax.
Exemple :
<!-- 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 wich is set to display: none; in css, and I want that after a click on .block2 the css become 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
.block1is set todisplay: flexby default (one click on.block2makes disappear.block1, the next click makes appear.block1).This doesn't work when
.block1is set by default ondisplay: none. But the function is correctly detect (I had set someconsole.log()message, and the css are update too).
For information :
- The css are initialised 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 ?