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, Link to comment https://forums.phpfreaks.com/topic/292972-onclick-function-alert-id-undefined/ 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. Link to comment https://forums.phpfreaks.com/topic/292972-onclick-function-alert-id-undefined/#findComment-1499033 Share on other sites More sharing options...
CroNiX Posted December 8, 2014 Share Posted December 8, 2014 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); }); Link to comment https://forums.phpfreaks.com/topic/292972-onclick-function-alert-id-undefined/#findComment-1499034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.