I have been trying to check whther a class exists inside a div. But it is always resulting a length 0. Here is my Code :-
<div id="accordion2" class="accordion" >
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
<div class="align-nav arrow-active">OSTOMY PRODUCTS</div></a>
</div>
<div id="collapseOne" class="accordion-body in collapse">
<div class="accordion-inner">
<ul>
<li><a href="#">Professional Resources</a></li>
<li><a href="#">One-piece Pouching</a></li>
</ul>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
<div class="align-nav arrow-inactive">LEARNING CENTER</div>
</a> </div>
<div id="collapseTwo" class="accordion-body collapse">
<div class="accordion-inner">
<ul>
<li><a href="#">Videos</a></li>
<li><a href="#">Before Your Surgery</a></li>
</ul>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
<div class="align-nav">PROFESSIONAL RESOURCES </div>
</a> </div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
<div class="align-nav">PROFESSIONAL RESOURCES </div>
</a> </div>
</div>
</div>
For the First 2 div $(this).find('accordion-body').length should be 1. But it still show 0. My Jquery COde is as Follows :-
$('.accordion-heading').click(function() {
if($(this).find('accordion-body').exist()) {
alert('hi');
if($(this).find('.collapsed').length > 0) {
alert('hi');
//$(this).find('.accordion-toggle .align-nav').removeClass('arrow-active').addClass('arrow-inactive');
}
else {
alert('hi');
//$(this).find('.accordion-toggle .align-nav').removeClass('arrow-inactive').addClass('arrow-active');
}
}
});
I need to get the $(this).find('accordion-body').length = 1; Please provide Solutions.
The hasClass() is an inbuilt method in jQuery which check whether the elements with the specified class name exists or not. Syntax: $(selector). hasClass(className);
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");
you should select a class selector in find
since your are searching for class .. and since accordion-body is parents descensent use parent()
try this
$(this).parent().find('.accordion-body').length == 1;
or you can use hasClass()
That would be '.accordion-body'
(the period is vital -- it is the class selector).
Also .exist()
is not a jQuery method, but I'll assume you've defined it somewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With