jquery - How to get top most div parent class -


i need main culpritclass div class holds after typed , enter pressed , put variable string.

i can't seem class.

html:

<div class="culpritclass"> <div class="inner" style="display: block;"> <div class="inner_chat"></div> <form> <textarea class="chat_text"></textarea>  </form>      </div><a class="culpritclass" href="#">culprit</a> </div> 

i tried following js undefined.

$('.chat_text').keypress(function(e) {     if (e.keycode == '13') {          var ru = $(this).prev().parent().attr('class');          alert(ru);     } }); 

the var ru line matters.

var ru = $(this).parent().parent().parent().attr('class'); 

should trick. though doesn't neatest solution, straightforward 1 imo. unless there's function pass argument function travels number up, e.g. ancestor(3) travel 3 levels in dom , return element.

but if trying find class itself, try closest.

var ru = $(this).closest('.culpritclass'); 

Comments