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!

3
  • 1
    I don't think "played" is a valid attribute for img tag... Commented Nov 4, 2012 at 17:34
  • Check this:stackoverflow.com/questions/1097522/… Commented Nov 4, 2012 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... Commented Nov 4, 2012 at 19:10

3 Answers 3

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

ps

better use 'data-played' as the attribute name

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments