can't read json items with jquery -
i've got unordered list customer names json file. want add click event, jquery can't seem read it. list looks in html source file, list items don't show in console.log
. manual added dummy customer handles click event fine.
html
<ul id="customers"> <li>dummy customer</li><!-- manually added test --> </ul>
js (filling unordered list)
var getcustomers = 'json_webservice_api_i_can't_share'; $.getjson( getcustomers ) .done(function( data ) { for(var = 0; < data.length; i++) { $('#customers').append('<li>' + data[i].name + '</li>'); } });
js (click event)
$('#customers li').click(function() { console.log($(this).text()); }
i'm guessing text inside list items filled customer names json file no real strings(?). or that. can help?
chances you're binding event when not have li elements
try
$(document).on('click', '#customers li', function() { console.log($(this).text()); });
also can done way, maybe better way (see jan dvorak comment)
$('#customers').on('click', 'li', function() { console.log($(this).text()); });
Comments
Post a Comment