Solved

Dynamic Creation of Text Boxes

Posted on 2003-11-25
7
306 Views
Last Modified: 2010-05-18
Hello folks !!

I just want a source code to create text boxes dynamically on click.

consider an example to create 10 text boxes on click of a button.

The prob.. I am facing across is, as soon as i click the btn, js rewrites the whole document and after the rewrite, the btn is not visible but only the text boxes.

suggest me a solution to retain the btn and create more text boxes on the same doc.

I am currently writing -

<html>
<head>
<script language="javascript">
       function gen()
       {
             for (t=0;t<10;t++)
             {
                   document.write("<input type='text' name='textbX" + t + "'>");
             }
       }
</script>
</head>
<body>

    <input type="button" onClick="gen();">

</body></html>
0
Comment
Question by:hemanthsharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 100 total points
ID: 9817961
Something like this?


<html>
<head>
<script language="javascript">
       function gen()
       {
             for (t=0;t<10;t++)
             {
                        textSection.innerHTML= textSection.innerHTML + "<input type='text' name='textbX" + t + "'><br>";
                   
             }
       }
</script>
</head>
<body>

    <input type="button" onClick="gen();">
      
      <div id='textSection'></div>

</body></html>


Greetz,
Didier
0
 
LVL 3

Expert Comment

by:etain
ID: 9818046
<html>
<head>
<script language="javascript">
       function gen()
       {
             for (t=0;t<10;t++)
             {
               var obj = document.createElement("input");
               obj.type='text'
               obj.name =  'textbX" + t        
               document.body.appendChild(obj);
             }
       }
</script>
</head>
<body>

    <input type="button" onClick="gen();">

</body></html>
0
 
LVL 17

Expert Comment

by:dorward
ID: 9818180
etain's code conforms to public standards, I suggest you use that.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:hemanthsharma
ID: 9818659
sorry... i could not accept etain's answer as it gave out an error.
0
 
LVL 17

Expert Comment

by:dorward
ID: 9818702
hmmm.... not sure if document.body is right come to think of it.

document.getElementsByTagName('body')[0].appendChild(obj); might work better
0
 
LVL 3

Expert Comment

by:etain
ID: 9824493
what error it give ??
The only know problem is u will have dup name when press the button more than one.
Case i didnt add the code to check it.
0
 

Expert Comment

by:hema3i
ID: 9857837
create a check box called "cbxSelectAll"
and on the check box add the following attribute

 onclick=javascript:checkAll(this.form)

<script type="text/javascript">
function checkAll(form)
{
  var thisNumRowsSelected = 0;
  var isChecked = document.all.cbxSelectAll.checked;
  for (var i=0; i < form.elements.length; i++)
  {
    if (form.elements[i].name.indexOf('_ctl') > -1)
    {
      var curElement = form.elements[i];
      if (isChecked)
      {
        curElement.checked = true;
        thisNumRowsSelected = thisNumRowsSelected + 1;
        while (!(curElement.tagName == "TR"))
        {
          curElement = curElement.parentElement;
        }
        if (form.elements[i].name != "cbxSelectAll")
        {
          curElement.style.backgroundColor = "gold";
        }
      }
      else
      {
        curElement.checked = false;
        while (!(curElement.tagName == "TR"))
        {
          curElement = curElement.parentElement;
        }
        if (form.elements[i].name != "cbxSelectAll")
        {
          curElement.style.backgroundColor = "#eeeeee";
        }
      }
    }
  }
  rowsSelected = thisNumRowsSelected;
}
            </script>
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question