javascript - Why would .append randomly fail in jquery? -
i'm using jquery add elements blank list.
on page have:
<ul id="mylist"> </ul>
and go through loop in script that's called dynamically created event handler. (it's "ondrop" of list item having been sorted drag operation)
var mylistitemhtml; (var = 0 ; < 5 ; i++) { mylistitemhtml += '<li id=listitem'+i+'>this item number'+i+'</li>'; } $('#mylist').append(mylistitemhtml);
and if check after...
if ($('#mylist li').length == 0 ) { alert('going crash since i'm expecting list items') }
roughly 95% of time list populated, 5% of time hit alert that's going cause exception later.
has run this? there callback or way know when/if append happens?
var mylistitemhtml; (var = 0 ; < 5 ; i++) { $('#mylist').append('<li id=listitem'+i+'>this item number'+i+'</li>'); }
try appending inside loop.
if ($('#mylist li').length == 0 ) { alert('going crash since i\'m expecting list items') }
you need \
before '
doesn't conflict.
edit: jsfiddle shows "undefined" http://jsfiddle.net/gyere/1/
Comments
Post a Comment