Here is jQuery condition if hasclass not working when i update class from Jquery, What is reason ? Means if i change / Update class then also Jquery if condition working on click, Rather it should not work after that.
in my case, first of all the div Some Content has class .L2D1 that's color is blue okay, then my jQuery condition is if Some Content has class .L2D1 means till its color is blue then it should should change the color of Some Other Content div that's red.
But when i changed the class .L2D1 from jQuery Onclick change class button, then it's class changed into .newClass then also my jQuery if condition working Why?
Where i am making mistake, plz make it solve. literally I don't able to get actual problem.
See this:
$('.ChangeClass').click(function () {
$(".L2D1").attr('class', 'newClass');
});
$(document).ready(function(){
if ($("#L2D1-id").hasClass("L2D1")) {
$('.ChangeColor').click(function () {
$(".Layout-2").addClass('intro');
});
}
});
.intro {
font-size: 120%;
color: red;
}
.newClass {
font-size: 120%;
color: green;
}
.L2D1 {
font-size: 100%;
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="L2D1" id="L2D1-id"> Some Content </div>
<div class="Layout-2"> Some Other Content </div>
<button class="ChangeClass">Change Class</button>
<button class="ChangeColor"> <!-- if ($("#L2D1-id").hasClass("L2D1")) Then { --> Change Color</button>
ifblock only runs once; when the page loads.ifblock within the event handler, like Mohamed-Yousef's original comment recommended.