Puzzled by failure of jQuery snippet for appending TDs to a TR -
the motivation behind question understand why jquery snippet below fails.
note: force of dumb, blind trial-and-error, have found alternative code work (or seems to). therefore, purpose of question not (per se) "fix" problem shown below. rather, goal understand as can why fails.
jquery(document).ready(function ($) { var tds = []; (var = 0; < 3; ++i) { var td = $('<td>'); tds.push(td); } var $tds = $(tds); var $row1 = $('#row1'); $tds.appendto($row1); });
basically, intent of snippet create 3 (jquery-wrapped) td
elements, , append them pre-existing tr
. (at end of post, copy full html+css+js code.)
as stated before, goal understand why snippet fails. therefore, question addressed can see, without running code, snippet fail. of can so: reasoning? tells snippet can't work?
here's full code:
<!doctype html> <html> <head> <title>title</title> <meta charset="utf-8"> <style> td{width:100px;height:20px;background:#c40000} </style> </head> <body> <table> <tr id="row1"></tr> </table> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> jquery(document).ready(function ($) { var tds = []; (var = 0; < 3; ++i) { var td = $('<td>'); tds.push(td); } var $tds = $(tds); var $row1 = $('#row1'); $tds.appendto($row1); }); </script> </body> </html>
you can create new jquery object array of domelements, not array of jquery
objects, line wrong:
var $tds = $(tds);
the simplest way fix changing $('<td>')
$('<td>')[0]
, actual <td>
domelement. use document.createelement('td')
there instead.
Comments
Post a Comment