Solved

Dynamic Creation of Text Boxes

Posted on 2003-11-25
7
303 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

775 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