Jump to content

onClick function: alert id = 'undefined'


slj90
Go to solution Solved by CroNiX,

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
Share on other sites

  • Solution

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);
});
Edited by CroNiX
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.