4

Can you please give me a hint on how do I get the value of a specific image by a custom attribute . For example, I have :

<img class="image" 
     identif="<?php echo $j; ?>" 
     id="<?php echo $id; ?>" 
     src="../<?php echo $sqlg[$pic] ?>" 
     h="<?php echo $heightl; ?>" 
     w="<?php echo $widthl ?>"
     height="<?php echo $height; ?>px" 
     width="<?php echo $width; ?>px" 
     title="Double-click to enlarge image" />

I want in jquery to get the "src" of the image by the "identif" attribute, thank you :)

southpaw93
  • 1,891
  • 5
  • 22
  • 39
  • possible duplicate of [jQuery how to find an element based on a data-attribute value?](http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value) and [Selecting element by data attribute](http://stackoverflow.com/questions/2487747/selecting-element-by-data-attribute) or maybe [How do I find all elements that have a title attribute using jQuery?](http://stackoverflow.com/questions/4186849/how-do-i-find-all-elements-that-have-a-title-attribute-using-jquery). – Felix Kling Jul 31 '12 at 12:08

2 Answers2

12

It's better to use instead a data-* attribute like so

<img class="image" data-identif="<?php echo $j; ?>"...

and thus you can retrieve that attribute with

$('img[data-identif]').data('identif');

See http://api.jquery.com/jQuery.data/ for further reference

If you need to grab the src of an img with data-identif = 5 just do like so:

$('img[data-identif="5"]').attr('src');
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • I don't think that's what the OP is trying to do. It seems he wants t find the image(s) with the `identif` attribute and get its/their `src` attribute. – Felix Kling Jul 31 '12 at 12:12
  • yeah , but I want to grab the src value from the image with the field lets say indentif is equal to 5 . – southpaw93 Jul 31 '12 at 12:14
2

jQuery has an attribute selector

$('img[data-identif="1"]')
Thomas
  • 8,426
  • 1
  • 25
  • 49