Yannick
Yannick

Reputation: 3653

jquery getting a specific class from many for a specific element

I have the following div element

<div id="row_1" class="active_page workorders_edit"></div>

And I want to be able to retrieve the class name workorders_edit. In my real case I only have access to the active_page class name

Please check my Fiddle for a better explanation. The end result should populate the id="classname" with only "workorders_edit" and not "active_page workorders_edit"

Thank you.

Upvotes: 0

Views: 47

Answers (2)

genesis
genesis

Reputation: 50966

http://jsfiddle.net/genesis/aaAyp/3/

var classname = $('.active_page').attr('class').split(' ')[1];

Note: only works when active_page class is first in the class attribute, and workorders_edit is the next class in the class attribute... any variation of this will break

Upvotes: 2

ipr101
ipr101

Reputation: 24226

This would work for your specific scenario -

$('#classname').text(classname.split(' ')[1]);

But it will only work while 'workorders_edit' is the second class name in a list of classes, if a new class was added to the start of the list of classes linked to your div it would no longer work.

Upvotes: 0

Related Questions