I need to get the first one without...">

JQuery: how to get one class without .split ()

I have a div with two classes.

<div class="one two"></div> 

I need to get the first one without a .split () callback.

 $(this).parent('div[class]').attr('class').split(' ')[0] 

Any idea?

Thanks in advance.

UPD : why do i need this? ) Because then I use split () , the code inspector in Chrome said for sure :

Uncaught TypeError: unable to call split method from undefined

and some function is not working properly.

+4
source share
3 answers

/\S*/.exec(this.parentNode.className)[0]; will give the first class of the parent class, if any.

This method is more efficient than your current method: Your method returns an empty string when the class attribute begins with a space.

+1
source

If you are really negative about using split() , you can combine indexOf () and zbbb () :

 var classes = $(this).parent("div[class]").attr("class"); var firstClass = classes.substr(0, classes.indexOf(" ")); 

However, the above code will only work if classes have more than one class name.

+1
source

I don’t know what you are trying to do with the information, but if you are checking if it has one , as a class, try to do it like this. I used the button since I needed a child to trigger an event;) http://jsfiddle.net/c3VdH/

0
source

Source: https://habr.com/ru/post/1391615/


All Articles