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.
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.
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/