-1

I have the following html on a page:

<span class="descriptionLink" projectid="14180">
            Some text
</span>

<span class="descriptionLink" projectid="14181">
            Some text
</span>

<span class="descriptionLink" projectid="14182">
            Some text
</span>

<span class="descriptionLink" projectid="14182">
            Some text
</span>

and i want to grab the item with projectid = 14182 . What is the right jquery syntax to grab this set of elements?

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • Use a standard attribute selector: `$("[projectid='14182']");` – Sampson Apr 02 '14 at 20:23
  • I think http://stackoverflow.com/questions/4146502/jquery-selectors-on-custom-data-attributes-on-html5 is a better duplicate, searching would have found a lot of answers. Also, to be valid HTML, you should use `data-projectid` instead. – Chris Baker Apr 02 '14 at 20:25

2 Answers2

2

seeing as projectid is an invalid element attribute, I'd say there's no right way to select on it. You'd be better off in HTML5 using

<span class="descriptionLink" data-projectid="14182"> 

and selecting off of $('[data-projectid="14182"]')

Or, alternatively, using classes:

<span class="descriptionLink projectid-14182">

Selecting like so:

var project_id_to_find = 14182;
$(".projectid-" + project_id_to_find);
  • when you say its an "invalid element" it seems to work in all browsers i have tried so what is "wrong" with it? – leora Apr 03 '14 at 23:17
  • 1
    Browsers tend to be forgiving. In XML you'd be able to define all of your own attributes, but HTML isn't really the same way. Here's the w3.org page on spans: http://www.w3.org/TR/html-markup/span.html and here is the list of 's valid attributes: http://www.w3.org/TR/html-markup/global-attributes.html . Even though browsers may seem to support it, it's behavior is basically undefined. –  Apr 09 '14 at 18:06
0

You can use attributes selector:

$('span[projectid=14182]')
Aguardientico
  • 7,641
  • 1
  • 33
  • 33