javascript - I cannot get the id of the span using jquery -
hi how can id of span using jquery. tried attempt it's not working nothing being log in console.
$(function () { (var id = 0; < data.length; i++) { var dynamicli = '<li class="">' + '<img src="path/to/image" class="_xy">' + data[i].name + '<i class="glyphicon glyphicon-user oncho"></i>' + '<span class="myspan" id="' + data[i].id + '></span></li>'; $('#myullist').append(dynamicli); } $('#myullist').on('click', '.oncho', function () { var id = $(this).find('span').attr('id'); console.log(id); }); }); //index.html
<div class="wrapper-n"> <ul id="myullist"></ul> </div> edit change click event class 'oncho' when click icon can span id.
edit: tried using find() still cannot id it's undefined
you missed completion quote of id:
'<span class="myspan" id="'+data[i].id+'"></span></li>'; // ^ update
use next span not descendent of .oncho:
$('#myullist').on('click', '.oncho', function () { var id = $(this).next('span').attr('id'); // ^^^^^ console.log(id); });
Comments
Post a Comment