slj90 Posted December 8, 2014 Share Posted December 8, 2014 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, Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 8, 2014 Share Posted December 8, 2014 Your function has no clue what "this" is. you'd need to pass "this" to your function. Quote Link to comment Share on other sites More sharing options...
Solution CroNiX Posted December 8, 2014 Solution Share Posted December 8, 2014 (edited) 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 December 8, 2014 by CroNiX 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.