Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7791405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:52:34+00:00 2026-06-01T21:52:34+00:00

I have a Element, that looks like this: <div id=test> <span>You have <em>30</em> <span>

  • 0

I have a 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 either with:

 <div id="test">
    <span>You reached the maximum length</span>
 </div>

or with

 <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 the set of new elements?
At the Moment I’m using to following function to remove all elements first:

function emptyElement(id) {
    while(document.getElementById(id).hasChildNodes()) {
        document.getElementById(id).removeChild(document.getElementById(id).firstChild)
    }
}

Afterwards I’m adding 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 have the impression that there might be an easier way for (1) removing the child-elements, (2) generationg the content and (3) adding the new elemnts to the div.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:52:35+00:00Added an answer on June 1, 2026 at 9:52 pm
    1. Removing can be done more easily by using element.innerHTML = '';
    2. Generating content can also be done more easily be either adding the content using .innerHTML, or caching a collection of elements.

      or

    3. elem.parentNode.replaceChild(new_elem, elem); on the #test element will be the easiest way.

    Using cached DOM elements

    Demo: 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);
            }
        };
    };
    

    Using hidden elements (recommended)

    Another method, which I recommend over the previous ones, is to have all elements in the and hide the unused messages using a CSS. This is more efficient than DOM manipulation, because the elements don’t have to be created over and over again.

    Demo: 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';
            }
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a messy html that looks like this: <div id=:0.page.0 class=page-element style=width: 1620px;>
I have an anchor element that looks like this <a href=url><img src=imgurl/>Text</a> I would
I have some data in an XML element that looks like this: <?xml version=1.0
I have a list of lists that looks like this: x[[state]][[year]] . Each element
I have a UL html list that looks like this: <div id=scroller> <ul> <li>item
I have code that looks like this: <div class=tag>Order # :</div> <div class=data> <input
I have some html that looks like this: <div class=block> <div id=item1></div> <div id=item2></div>
So, I have an HTML string that looks just like this: <div> <div class=venue-tooltip>
I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A
I have a document that looks something like <root> <element> <subelement1 /> <subelement2 />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.