How can i use jquery to find an item by a different attribute than class? [duplicate]

StackOverflow https://stackoverflow.com/questions/22822191

سؤال

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?

هل كانت مفيدة؟

المحلول

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);

نصائح أخرى

You can use attributes selector:

$('span[projectid=14182]')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top