0

How can I find class from

<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
    <i class="ace-icon fa fa-angle-double-right" data-icon2="ace-icon fa fa-angle-double-right" data-icon1="ace-icon fa fa-angle-double-left"></i>
</div>

I want to find ace-icon fa fa-angle-double-right I have tried this

    var sidebar_collapse = document.getElementById("sidebar-collapse");
    $(sidebar_collapse).on('click', function () {
        var angel_double_right = $(sidebar_collapse).find('.ace-icon fa fa-angle-double-right');
        if (angel_double_right) {
            alert("yeah has class");
            $(ticksign).css('display', 'none');
        }
    });

But not working. It alert every time

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    `if (angel_double_right.length)`. jQuery objects are always *truthy*, even if they don't contain any elements. – Phil Apr 28 '15 at 05:14
  • 1
    possible duplicate of [Is there an “exists” function for jQuery?](http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) – Phil Apr 28 '15 at 05:16
  • Also, your selector should be `'.ace-icon.fa.fa-angle-double-right'` (there was an answer with this but it's been removed) – Phil Apr 28 '15 at 05:18

3 Answers3

1

.find() returns a jQuery object, which evaluates to true every time. So your if condition will always be true. If you need to check if any elements actually matched the selector:

if (angel_double_right.length) {
Victor Stanciu
  • 12,037
  • 3
  • 26
  • 34
0

Access the child and then get the class name:

document.write(JSON.stringify(document.getElementById("sidebar-collapse").children[0].className));
<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
    <i class="ace-icon fa fa-angle-double-right" data-icon2="ace-icon fa fa-angle-double-right" data-icon1="ace-icon fa fa-angle-double-left"></i>
</div>
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

Try this

$("sidebar-collapse").on('click', function () {
    var angel_double_right = $(this).find('.ace-icon.fa.fa-angle-double-right');
    if (angel_double_right.length>0) {
        alert("yeah has class");
        $(ticksign).css('display', 'none');
    }
});
Rahaman
  • 323
  • 1
  • 9
  • if one element has more than one class name than write as below .ace-icon.fa.fa-angle-double-right – Rahaman Apr 28 '15 at 05:24