user4729539
user4729539

Reputation:

Find class name

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

Upvotes: 0

Views: 97

Answers (3)

Rahaman
Rahaman

Reputation: 333

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');
    }
});

Upvotes: 0

Vic
Vic

Reputation: 12517

.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) {

Upvotes: 1

A.J. Uppal
A.J. Uppal

Reputation: 19264

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>

Upvotes: 0

Related Questions