Jump to content

onClick function: alert id = 'undefined'


slj90

Recommended Posts

When I click on this span I want it to alert the ID if the span...

<span id='" . $thisid . "' onClick='likeThis()'><img src='https://cdn1.iconfinder.com/data/icons/nuvola2/32x32/apps/amor.png'>Like</center></span>
<script>
function likeThis(){
    var lb = $(this).attr('id');
alert(lb)
}
</script>

However, it currently just says 'undefined'.

 

What's wrong?

Thanks,

 

Link to comment
https://forums.phpfreaks.com/topic/292972-onclick-function-alert-id-undefined/
Share on other sites

But it would be much better to not use inline javascript anyway. I'd give your span a special class, and then create a click event using that.

<span class="specialClass" id="my-id">some span</span>
<span class="specialClass" id="different-id">some other span</span>
$('.specialClass').click(function(e){
  var id = $(this).attr('id');
  alert(id);
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.