Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamic Creation of Text Boxes

Posted on 2003-11-25
7
Medium Priority
?
309 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

773 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