How to replace element content with DOM manipulation functions in JavaScript?
I have an element that looks like this:
<div id="test">
<span>You have
<em>30</em>
<span> characters left</span>
</div>
and I want to replace it with either:
<div id="test">
<span>You reached the maximum length</span>
</div>
or
<div id="test">
<span>Your text is </span>
<em>30</em>
<span> characters to long</span>
</div>
Is there a way to replace all child elements with a set of new elements? At the moment of Moment, I use the following function to remove all elements first:
function emptyElement(id) {
while(document.getElementById(id).hasChildNodes()) {
document.getElementById(id).removeChild(document.getElementById(id).firstChild)
}
}
Subsequently, I add the content again:
span1 = document.createElement("span");
span1.appendChild(document.createTextNode("You have "));
len = document.createElement("em");
len.appendChild(document.createTextNode(length));
span2 = document.createElement("span");
span2.appendChild(document.createTextNode(" characters left."));
// remove child elements
emptyElement('test');
// Add content
document.getElementById('test').appendChild(span1);
document.getElementById('test').appendChild(len);
document.getElementById('test').appendChild(span2);
I get the impression that there might be an easier way: (1) to remove child elements, (2) generate content and (3) add new elements to the div.
+3
4 answers
- Removing can be done using
element.innerHTML = ''; - You can also generate content easily by adding content using
.innerHTMLor caching a set of elements.
or elem.parentNode.replaceChild(new_elem, elem);#test.
DOM
: http://jsfiddle.net/aKASN/1/
HTML:
<input type="text" id="input">
<div id="test"></div>
JavaScript:
window.onload = function() {
// References
var limit_reached = document.createElement('div');
limit_reached.id = 'test';
limit_reached.innerHTML = 'You reached the maximum length';
var chars_left = document.createElement('div');
chars_left.id = 'test';
chars_left.innerHTML = 'You have <em id="number_of_chars">30</em> characters left';
document.getElementById('input').onkeyup = function() {
var char_count = this.value.length;
var limit = 20;
var test = document.getElementById('test');
if ( char_count > limit ) {
test.parentNode.replaceChild(limit_reached, test);
} else {
chars_left.getElementsByTagName('em')[0].innerHTML = limit - char_count;
test.parentNode.replaceChild(chars_left, test);
}
};
};
()
, , , CSS. , DOM, .
: http://jsfiddle.net/aKASN/
CSS
.hidden {display:none;}
HTML:
<input type="text" id="input">
<div><!-- Original id="test" -->
<span id="charsleft" class="hidden">
You have
<em id="number_of_chars">30</em>
characters left
</span>
<span id="limit_reached" class="hidden">
You reached the maximum length
</span>
</div>
JavaScript:
window.onload = function() {
// References
var chars_left = document.getElementById('charsleft');
var number_of_chars = document.getElementById('number_of_chars');
var limit_reached = document.getElementById('limit_reached');
var input = document.getElementById('input');
input.onkeyup = function() {
// Example value:
var char_count = input.value.length;
// Example: 20 character limit
if ( char_count > 20 ) {
chars_left.className = 'hidden';
limit_reached.className = '';
} else {
chars_left.className = '';
number_of_chars.innerHTML = 20 - char_count;
limit_reached.className = 'hidden';
}
};
};
+4
If your content is not a large number of .innerHTML users
<div id="test">
<span>You have
<em>30</em>
<span> characters left</span>
</div>
can be replaced by
<div id="test">
<span>You reached the maximum length</span>
</div>
using
document.getElementById('test').innerHTML = '<span>You reached the maximum length</span>';
-1