1

Possible Duplicate:
jquery v1.3.2 find element by attribute
Is it possible to grab a link by its href if it doesn’t have a class or ID?

My question revolves around having an attribute to an element (can't use ID's as I can' be certain that those will be unique) -

for instance, I have this:

<a href="someurl.com">Link</a>

but I can dynamically inject and could I possibly have a feasible/fast/efficient way of referencing this in the dom (keep in mind, there would be numerous links like this).

<a href="someurl.com" data-access-label="000998">Link</a>

So, I will be reiterating thru a hash and curious if this would be fast/efficient?

data = {
  '98789' : 'yeah',
  '871637':'cool',
  '00198789' : 'sure thing',
  '871609':'no way',
  '000998':'alright'
}

So, it seems inefficient to me that I would .each thru all the links then find the data-access-label value, and then what? could I possible bind those hrefs or something so that once I run my function - it would look like this

 <div>
   <a href="someurl.com" data-access-label="000998">Link</a> alright
       <span> <a href="someurl.com" data-access-label="871637">Link</a> cool</span>
 </div>

At any given time there will be only upwards of 40 items in the data hash, so perhaps I could ".each" thru it, and match the key with the value in those data-access-label ? But my want is perhaps directly accessing that item rather than reiterating thru href's too.

Community
  • 1
  • 1
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • Why can't you use `id="access-label-000998"`? – dfsq Oct 29 '12 at 18:20
  • Could you specify what you're trying to achieve? Are those elements already in the DOM? If so use an attribute selector. – Fabrício Matté Oct 29 '12 at 18:20
  • ok, so use a class name? – SpYk3HH Oct 29 '12 at 18:20
  • @fabricio - yes, the elements are in the dom. My *data* hash is built from an ajax call I do - and that associated text is just some additional descriptor for the link. I can have n number of links on the page, BUT some will have this data-access-label atrribute. Not all of the links on the page. I have some predetermined conditionals to determine. But the "value" of that lable *should* be unique - thus the number. If that number matches the key in the data hash - the I append the text. – james emanon Oct 29 '12 at 18:33

3 Answers3

4

Using jQuery's attribute selector:

var link = $('[data-access-label*="' + identifier + '"]');

alert(link.text());
Alex
  • 34,899
  • 5
  • 77
  • 90
2

$("a[data-access-label]")

That will return an array of all a elements with that property.

You can then call the .each() for further processing.

http://api.jquery.com/has-attribute-selector/

karljacuncha
  • 164
  • 2
  • 4
0
$("a").each(function(i) { 
    if (typeof $(this).attr('name') !== 'undefined' && $(this).attr('name') !== false) {
        // do work
    };
})

OR

$("a").each(function(i) { 
    if (typeof $(this).data('access-label') == "000998") {
        // do work
    };
})

OR

$('[data-access-label*="' + indexValue + '"]').click(function(e) { /* do work */ });
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81