user1355300
user1355300

Reputation: 4987

Get class of an element inside a link

I have an icon element inside a link, how can I get its css class with jQuery (eg. icon).

<a class="myclass"><i class="icon"></i>text</a>

I can get the text

var txt = $('.myclass').text();

but unable to get the class of the i.

Upvotes: 1

Views: 38

Answers (2)

Pratik Joshi
Pratik Joshi

Reputation: 11693

working FIDDLE Way1

Now FIDDLE Way2

Use folowing

var txt = $('.myclass > i').prop('class');
alert(txt);

Means inside class myclass find i , and then find its class property, You can also use attr.

OR

var txt = $('.myclass').find('i').prop('class');
alert(txt);

Upvotes: 2

Kiran
Kiran

Reputation: 20313

Try this:

$('.myclass > i').attr('class');

Upvotes: 1

Related Questions