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 9184071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:57:24+00:00 2026-06-17T18:57:24+00:00

i am using innerHTML to add text boxes dynamically. The code sample is as

  • 0

i am using innerHTML to add text boxes dynamically. The code sample is as follows:

<html>
<head>
  <script type="text/javascript" >
  var i=0;
  function add()
  {
    var tag = "<input type='text' name='" + i + "' /> <br/>";
    document.getElementById("y").innerHTML += tag;
    i++;
  }
  </script>
</head>

<body>
  <input type="button" id="x" value="Add" onclick="add();" />
  <div id="y"></div>
</body>

</html

Are there any ways to add text boxes dynamically without losing values of previous text box when a new text box is added?
Similar question has been posted, but there are no answers 🙁

What if I want to add textbox in this situation:

function add() {
   var element='<li class="ie7fix" style="width:620px;"><div class="m_elementwrapper" style="float:left;"><label class="fieldlabel" style="width:106px;float:left;padding-top:3px;" for="p1f4"><span><span class="pspan arial" style="text-align:right;font-size:14px;"><span class="ispan" xml:space="preserve"></span></span></span></label><div style="float:left;width:475px;" class="m_elementwrapper"><input type="text" style="font-family:Arial, Helvetica, sans-serif;font-size:14px;width:244px;max-width:244px;" name="' + i + '"  class="fieldcontent"><div class="fielderror"></div></div></div><div style="clear:both;font-size:0;"></div></li>';
   document.getElementById("addskills").innerHTML += element;
   i++;
}
  • 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-17T18:57:25+00:00Added an answer on June 17, 2026 at 6:57 pm

    Yes, through DOM Manipulation:

    function add() {
        var tag = document.createElement('input'); // Create a `input` element,
        tag.setAttribute('type', 'text');          // Set it's `type` attribute,
        tag.setAttribute('name', i);               // Set it's `name` attribute,
    
        var br = document.createElement('br');     // Create a `br` element,
    
        var y = document.getElementById("y");      // "Get" the `y` element,
        y.appendChild(tag);                        // Append the input to `y`,
        y.appendChild(br);                         // Append the br to `y`.
        i++;
    }
    

    This doesn’t trigger the browser’s DOM parser like a innerHTML does, leaving everything intact.

    (innerHTML forces the browser to re-parse the entire DOM, because anything could be added with innerHTML, so the browser can’t predict anything, in contrast to adding a node to a element.)

    Now, to add this:

    <li class="ie7fix" style="width:620px;">
        <div class="m_elementwrapper" style="float:left;">
            <label class="fieldlabel" style="width:106px;float:left;padding-top:3px;" for="p1f4">
                <span>
                    <span class="pspan arial" style="text-align:right;font-size:14px;">
                        <span class="ispan" xml:space="preserve">
                        </span>
                    </span>
                </span>
            </label>
            <div style="float:left;width:475px;" class="m_elementwrapper">
                <input type="text" style="font-family:Arial, Helvetica, sans-serif;font-size:14px;width:244px;max-width:244px;" name="' + i + '"  class="fieldcontent">
                <div class="fielderror">
                </div>
            </div>
        </div>
        <div style="clear:both;font-size:0;">
        </div>
    </li>
    

    You’ll need:

    function add() {
        // Create elements
        var d1 = c('div'),  s1 = c('span'), ip = c('input'),
            d2 = c('div'),  s2 = c('span'), li = c('li'),
            d3 = c('div'),  s3 = c('span'), la = c('label'),
            d4 = c('div');
        // You can "chain" `appendChild`.
        // `li.appendChild(d1).appendChild(la);` is the same as `li.appendChild(d1); d1.appendChild(la);`
        li.appendChild(d1).appendChild(la).appendChild(s1).appendChild(s2).appendChild(s3);
        d1.appendChild(d2).appendChild(ip);
        d2.appendChild(d3);
        li.appendChild(d4);
    
        setAttributes(
            [li, d1, la, s2, s3, d2, ip, d3, d4],
            [
                {'class':"ie7fix",              'style':"width:620px;"  },
                {'class':"m_elementwrapper",    'style':"float:left;"   },
                {'class':"fieldlabel",          'style':"width:106px;float:left;padding-top:3px;", 'for':"p1f4" },
                {'class':"pspan arial",         'style':"text-align:right;font-size:14px;"  },
                {'class':"ispan",               'xml:space':"preserve"  },
                {'class':"m_elementwrapper",    'style':"float:left;width:475px;"   },
                {'class':"fieldcontent",        'type':"text",      'style':"font-family:Arial, Helvetica, sans-serif;font-size:14px;width:244px;max-width:244px;", 'name':''+i},
                {'class':"fielderror"   },
                {'style':"clear:both;font-size:0;"  }
            ]
        );
        var br = document.createElement('br');     // Create a `br` element,
        var y = document.getElementById("y");      // "Get" the `y` element,
        y.appendChild(li);                         // Append the input to `y`,
        y.appendChild(br);                         // Append the br to `y`.
        i++;
    }
    
    // Apply a array of attributes objects {key:value,key:value} to a array of DOM elements.
    function setAttributes(elements, attributes){
        var el = elements.length,
            al = attributes.length;
        if(el === al){
            for(var n = 0; n < el; n++){
                var e = elements[n],
                    a = attributes[n];
                for(var key in a){
                    e.setAttribute(key, a[key]);
                }
            }
        }else{
            console.error("Elements length " + el + " does not match Attributes length " + al);
        }
    }
    
    // Alias for shorter code.
    function c(type){
        return document.createElement(type);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JavaScript to dynamically add rows to a table, I create some textboxes
var a=0; function _add_more() { var txt = <br><input type=\file\ name=\item_file[]\><br><input type=\text\ name=\text+a+\>; document.getElementById(dvFile).innerHTML
You can add as many text boxes as you want as shown by using
Most of the experience I have manipulating HTML elements using Javascript involves the innerHTML
I am looking for a way to add an HTML element using JavaScript. But
How can I can I alter (change, add, whatever) HTML/text real-time using the input
I am having issues passing a dynamic parameter to a JavaScript function using innerHTML.
i am developing a gadget and i am using JavaScript to change innerHTML of
I dynamically add rows to divStaff using jquery: $(span[id$='lblAddStaff']).click(function() { //$(.staff_tpl).find(input[id$='txtRate']).val(0,00); var staff_row =
I am using the following JavaScript to add a number of inputs based on

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.