Pawan
Pawan

Reputation: 32331

How to find current elements root div class?

When clicked on a checkbox , i am trying to find the root div class of that element

I was trying with different options ,but for all of them i am getting undefined .

$(document).on("click", ".checkboxclas", function(e) {
    var cls = $(this).find('div:first').attr('class') ;
    alert(cls)

});

When clicked on activateUiHTML checkbox, i wanted to display activateUiHTMLparentdivclass

When clicked on ordersdiv checkbox , i wanted to display parentsordersdivclass

http://jsfiddle.net/e56TY/31/

could anybody please tell me how to do this .

Upvotes: 2

Views: 537

Answers (2)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use .parents().last() to find the last parent element, I think you have some confusion with what is a root element is.

$(document).on("click", ".checkboxclas", function(e) {
    var cls = $(this).parents('div').last().attr('class') ;
    alert(cls);
});

DEMO

Upvotes: 2

Think Different
Think Different

Reputation: 2815

change

var cls = $(this).find('div:first').attr('class') ;

to

var cls = $(this).closest('div').attr('class') ;

Fiddle Example

Upvotes: 1

Related Questions