Insert after with Dom Javascript

English | js/ajax - 03/11/2005 @ 11:39



Velocissimo e molto utile:

function insertAfter(parent, node, referenceNode)
{
parent.insertBefore(node, referenceNode.nextSibling);
}


5 Responses to “Insert after with Dom Javascript”

Jonathan Andrew WolterNo Gravatar says on

Thanks,

Helpful. I found you on google as the 1st result searching for “javascript insert after”

Cheers,

jaw

WesNo Gravatar says on

Another useless snippet of code.

referenceNode.nextSibling will produce an empty textnode in Firefox if your code is indented properly, causing your script to add the element in the wrong place.

If you want to find the next sibling for real, use a genuine function:

function get_next_sibling(elem) {
if(elem.nextSibling) {
var sib = elem.nextSibling;
while(sib.nodeType != 1) {
if(sib.nextSibling) {
sib = sib.nextSibling;
} else {
return false;
}
} return sib;
} return false;
}

baldoNo Gravatar says on

thanks Wes.. I’ll try it!

Dee @ Ecco DiscountNo Gravatar says on

Hello there, just stopped by doing some research for my Ecco website. Can’t believe the amount of information out there. Wasn’t what I was looking for, but great site. Cya later.

Sabra WalstonNo Gravatar says on

This is a great write-up, I discovered your webpage researching google for a similar content and arrived to this. I couldnt come across to much other details on this blog post, so it was great to discover this one. I will certainly be returning to check out some other articles that you have another time.

Commenta | Leave a Reply

CommentLuv Enabled
***