Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery, getting image with custom attribute

Tags:

jquery

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

like image 445
southpaw93 Avatar asked Dec 11 '22 23:12

southpaw93


2 Answers

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');
like image 103
Fabrizio Calderan Avatar answered Feb 23 '23 01:02

Fabrizio Calderan


jQuery has an attribute selector

$('img[data-identif="1"]')
like image 30
Thomas Avatar answered Feb 22 '23 23:02

Thomas