Check If Dynamically Created Element Has Class
I want to check if an element exists on the page with a specified class (that's created dynamically.) The simplest would be to do this, but since the elements doesn't exist on the
Solution 1:
Are you adding the element to the DOM after the page is ready? If you so, you can just check if the element has the class as you described in your question.
However, if the element is being added before the DOM is ready, simply do this:
$(document).ready(function () {
if($('.list li').hasClass('aDynamicallyGeneratedClass')){
//Then do this with my object.
});
});
When the DOM is ready, then this function will fire.
I hope this helps!
Cheers!
Solution 2:
Wait till document is loaded
$(document).ready(function() {
if ($('.list li').hasClass('aDynamicallyGeneratedClass')) {
console.log('exists');
}
});
Solution 3:
Try this:
if ( $('.list li.YOUR_CLASS').length > 0)
{
//Then do this with my object.
}
Replace YOUR_CLASS
with what you want.
Solution 4:
You can simply do something like below:
$(document).ready(function() {
if ($(".choose").is(".aDynamicallyGeneratedClass")) {
$(".choose").css('color', 'red'); // Or something else
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="choose aDynamicallyGeneratedClass">One</li>
</ul>
This is just an example, study and adjust as needed
Post a Comment for "Check If Dynamically Created Element Has Class"