object - Javascript - get number of elements in Document Fragment -
i'm creating counter passing number following function:
function numbertohtml(n) { function createspan(n) { var span = document.createelement('span'); span.textcontent = n; return span; } function iter(n, container) { if (n === '') return container; container.insertbefore(createspan(n.slice(-3)), container.firstchild); return iter(n.slice(0,-3), container); } return iter(string(n), document.createdocumentfragment()); }
this returns:
documentfragment [ <span>, <span> ]
i'm havibng trouble number of elements in documentfragment. tried $(myobject).length
, returns 1.
how can no of items in documentfragment?
sorry first answer wasn't correct. had trouble figuring out, how got outcome (that have been helpfull). looking is:
var myobject = numbertohtml(3231123123); var length = $(myobject).children().length; alert(length); // 4
or juhana pointed out, can omit jquery wrapper:
var myobject = numbertohtml(3231123123); var length = myobject.children.length; alert(length); // 4
since span elements added children, want count children of container.
Comments
Post a Comment