0

I want to do following: Have an tag inside an tag. listen to click() event of a tag. in click-event i want to check if img tag has an specific attribute or not.

for example:

<a href='#' id='myid'><img src='' alt='' played='1'></a>


$('#myid').click(function(e){
  if(e.target.attr('played')){
    // do something...
  }
});

how can i get the attribute of my img tag? i never check the element-handlers in jquery...

kind regards!

Fabian
  • 1,806
  • 5
  • 25
  • 40
  • 1
    I don't think "played" is a valid attribute for img tag... – Andrea Ligios Nov 04 '12 at 17:34
  • Check this:http://stackoverflow.com/questions/1097522/check-existence-of-an-attribute-with-jquery – Vucko Nov 04 '12 at 17:34
  • yeah i found that thread, but it didnt match my needs. andrea, what else do you suggest if you need to store data to an element? i know this is not the best solution but i didnt found any better... – Fabian Nov 04 '12 at 19:10

3 Answers3

2
$('#myid').click(function(e){
  if( $(this).find('img').attr('played') == 1)
  {
    // do something...
  }
});

ps

better use 'data-played' as the attribute name

Kirill Ivlev
  • 12,310
  • 5
  • 27
  • 31
1

You can do this:

$('#myid').click(function(e){
  e.preventDefault(); // stopping default behaviour of the anchor
  var played_attribute = $(this).find(img).attr('played');
  if(played_attribute == '1') {
   //attribute had value of 1
  } else {
   //attribute either never existed or didnt have value of 1
  }
});

Hope it helps.

Marko Francekovic
  • 1,450
  • 10
  • 10
0

You should be doing $(e.target).attr("played")

mfreitas
  • 2,395
  • 3
  • 29
  • 42