Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery Attr() to select only first attribute?

Tags:

jquery

I have a div as follows:

<DIV CLASS="variable productPopup"></DIV>

When assigning it to a var, how do I only select variable? Please note that variable changes, so it needs to select the first item, not literally the words "variable"

var ID = $(this).attr("class");
like image 421
Jared Avatar asked Dec 13 '22 00:12

Jared


1 Answers

To get the first class mentioned, you can split the value by spaces:

var firstClass = $(this).attr("class").split(" ")[0];

So if the class attribute was "foo Bar", you'd get "foo".

like image 103
Sampson Avatar answered Dec 15 '22 12:12

Sampson