Fabian
Fabian

Reputation: 1836

jQuery click event

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!

Upvotes: 0

Views: 78

Answers (3)

mfreitas
mfreitas

Reputation: 2405

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

Upvotes: 0

Marko Francekovic
Marko Francekovic

Reputation: 1480

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.

Upvotes: 1

user1796666
user1796666

Reputation:

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

ps

better use 'data-played' as the attribute name

Upvotes: 2

Related Questions