最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove an individual text node in a div without removing all of them? - Stack Overflow

programmeradmin2浏览0评论

How do I go about removing an individual dynamically created text node?

I am generating input's on the fly and am using .createTextNode to place descriptive text before the elements. I need the ability to delete specific elements that are being created and am using .removeChild to do it. That works fine for removing an individual input because I have something to reference (id/name). Is there a way to set some sort of reference to each text node so I can delete it along with its corresponding input control?

var box = document.getElementById("myDiv");

box.appendChild(document.createTextNode('Status: '));
var inp = document.createElement('input');
inp.type = 'text';
// add attributes, etc...

box.appendChild(inp);

How do I go about removing an individual dynamically created text node?

I am generating input's on the fly and am using .createTextNode to place descriptive text before the elements. I need the ability to delete specific elements that are being created and am using .removeChild to do it. That works fine for removing an individual input because I have something to reference (id/name). Is there a way to set some sort of reference to each text node so I can delete it along with its corresponding input control?

var box = document.getElementById("myDiv");

box.appendChild(document.createTextNode('Status: '));
var inp = document.createElement('input');
inp.type = 'text';
// add attributes, etc...

box.appendChild(inp);
Share Improve this question edited Dec 21, 2009 at 21:03 Crescent Fresh 117k27 gold badges157 silver badges140 bronze badges asked Dec 21, 2009 at 20:54 JadenJaden 1892 gold badges2 silver badges11 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 6

Why not wrap both in a fieldset to begin with?

var box = document.getElementById("myDiv");

var field = document.createElement('fieldset');
field.appendChild(document.createTextNode('Status: '));
var inp = document.createElement('input');
inp.type = 'text';
// add attributes, etc...

field.appendChild(inp);
box.appendChild(field);

This way just removing the field element will remove both your textnode and your input at the same time.

Keep reference to it:

var txt = document.createTextNode('Status: ');
box.appendChild(txt);

and then remove with:

txt.parentNode.removeChild(txt);

If node is supposed to be immediately before the input, this will work as well:

inp.parentNode.removeChild(inp.previousSibling);

It should work if you don't use innerHTML or normalize(), which could cause nodes to be re-created or merged, invalidating your references.

In case you wanted to remove arbitrary text from a node, there's textnode.splitText(offset).

Do not remove text nodes that are part of a list of textnodes in the DOM. Even if you reference them (before you appended them to the DOM).

The browser may merge multiple text nodes! I am not sure what the standards state, but its possible - at least some experience told me.. (may be old browsers, but it was the case).

Instead of that, you could either wrap each text node in a span or div tag, or you could use some kind of text replacements. I'd prefer the former.

If you change this line:

box.appendChild(document.createTextNode('Status: '));

To:

var textNode = document.createTextNode('Status: ');
box.appendChild(textNode);

Then you can refer to the textNode var later to delete it. Be careful, though--some browsers will merge adjacent text nodes.

var box = document.getElementById("myDiv");

var label = document.createTextNode('Status: ')
box.appendChild(label);
var inp = document.createElement('input');
inp.type = 'text';
// add attributes, etc...

box.appendChild(inp);

// do other stuff

//remove the label
label.nodeValue = "";
发布评论

评论列表(0)

  1. 暂无评论