Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use quotes in jQuery when searching for attribute values?

If I have a link like this:

<a data-btn="login"></a>

If I wished to select in jQuery using the data attribute which should I do?

var a = $("[data-btn='login']");  //This?

var a = $("[data-btn=login]");    //Or this?
like image 222
C.J. Avatar asked Apr 03 '12 04:04

C.J.


People also ask

How to find attribute name in jQuery?

To get the name, you'd use $(selector). attr('name') which would return (in your example) 'xxxxx' . Save this answer.


1 Answers

As of jQuery 1.5, both approaches will work, and function identically.

jQuery used to require quotes around attribute values, so your second selector won't work with previous versions. If you're stuck with an older version for some reason, use the first selector.

like image 153
BoltClock Avatar answered Oct 21 '22 04:10

BoltClock