How can I append me element as the fourth one in my long list of elements. Right now it is being appended to the bottom of all my elements.
pm_form.appendChild(p_wrap);
How can I append me element as the fourth one in my long list of elements. Right now it is being appended to the bottom of all my elements.
pm_form.appendChild(p_wrap);
If pm_form is a form you can use the form’s elements array to locate the insertion point.
pm_form.insertBefore(p_wrap,pm_form.elements[4])
You can do this with collections like images and links as well, but most containers contain whitespace nodes that may throw off your count.
For these you can use a getElementsByTagName list, if there is no nesting.
With nested elements you’d use the childNodes array of the containing element, and check each node until you match your tag 4 times.
var pa= container, node= pa.firstChild, count= 0;
while(node && count<4){
if(node.tagName== 'SPAN') count++;
node= node.nextSibling;
}
if(node) pa.insertBefore(newnode, node);
else pa.appendChild(newnode)