SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Append as fourth element

    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.

    Code JavaScript:
    pm_form.appendChild(p_wrap);

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    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)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •