javascript - Why cant it find the id value by jQuery -


i making app takes url json file , based on urls, data soundcloud api. far there no problem. when loading data, put each 1 of them in <li> tag. , each tag have id value based on gathered data. want get value of each clicked <li> tag. not work. think because made dynamically, not know solution it.

here code loading data: , function showing clicked <li> in console:

$.each(data.playlistarray, function(key, val){                 var track = answer[val.url];                 var tracktitle = track.title;                      $(".playerlist .list").append(                     "<li id=" + track.id +">" +                      "<h3 class=\"songtitle\">" + track.title + "</h3>" +                     "</li>"); });  $(".playerlist .list li").click(function(){     console.log($(this).attr('id')); }); 

and demo full code: http://jsfiddle.net/danials/3jsje/3/

any idea?

you need write event li inside class list , have use event delegation using on() li generated dynamically via ajax, this:

    $(".playerlist .list").on('click','li',function(){     console.log(this.id); }); 

updated fiddle


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -