-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?

2
  • Use a standard attribute selector: $("[projectid='14182']"); Commented Apr 2, 2014 at 20:23
  • I think stackoverflow.com/questions/4146502/… is a better duplicate, searching would have found a lot of answers. Also, to be valid HTML, you should use data-projectid instead. Commented Apr 2, 2014 at 20:25

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

when you say its an "invalid element" it seems to work in all browsers i have tried so what is "wrong" with it?
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: w3.org/TR/html-markup/span.html and here is the list of <span>'s valid attributes: w3.org/TR/html-markup/global-attributes.html . Even though browsers may seem to support it, it's behavior is basically undefined.
0

You can use attributes selector:

$('span[projectid=14182]')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.