473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

adding elements

14
Need help on this one, I have this code where i intend to use, it adds a new textarea each time a button is clicked. How can i assign a unique element name for each textarea created?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function addTextArea() 
  3. {
  4.     var newInput = document.createElement("textarea");
  5.     newInput.setAttribute("type", "text");
  6.     newInput.setAttribute('id','idName');
  7.     newInput.setAttribute('name','name');
  8.     newInput.setAttribute("cols", "50");
  9.     newInput.setAttribute("row", "30");
  10.     document.getElementById("divToAddTextBox").appendChild(newInput);
  11. }
  12. </script>
  13. <body>
  14. <form name ="reg" method="POST" action="sample_a.php">
  15. <table><tr><td  id="divToAddTextBox" width="400">
  16. </td></tr>
  17. <input type="button" onclick="addTextArea()" value="Click Here to Add Textbox" />
  18. <input type="button" onclick="submit()" value="Click Here Submit" />
  19. </table>
  20. </body>
Jul 17 '07 #1
5 1286
epots9
1,351 Expert 1GB
Need help on this one, I have this code where i intend to use, it adds a new textarea each time a button is clicked. How can i assign a unique element name for each textarea created?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function addTextArea() 
  3. {
  4.     var newInput = document.createElement("textarea");
  5.     newInput.setAttribute("type", "text");
  6.     newInput.setAttribute('id','idName');
  7.     newInput.setAttribute('name','name');
  8.     newInput.setAttribute("cols", "50");
  9.     newInput.setAttribute("row", "30");
  10.     document.getElementById("divToAddTextBox").appendChild(newInput);
  11. }
  12. </script>
  13. <body>
  14. <form name ="reg" method="POST" action="sample_a.php">
  15. <table><tr><td  id="divToAddTextBox" width="400">
  16. </td></tr>
  17. <input type="button" onclick="addTextArea()" value="Click Here to Add Textbox" />
  18. <input type="button" onclick="submit()" value="Click Here Submit" />
  19. </table>
  20. </body>
try this:
Expand|Select|Wrap|Line Numbers
  1. var count = 1;
  2. function addTextArea() 
  3. {
  4.     var newInput = document.createElement("textarea");
  5.     newInput.setAttribute("type", "text");
  6.     newInput.setAttribute('id','textarea' + count);
  7.     newInput.setAttribute('name','textarea' + count);
  8.     newInput.setAttribute("cols", "50");
  9.     newInput.setAttribute("row", "30");
  10.     document.getElementById("divToAddTextBox").appendChild(newInput);
  11. count++;
  12. }
  13.  
** make sure your script tag is between the head tag.

so now every textarea is called the same just with a different number at the end, makes it easier to read after cuz then u can use a for loop.

good luck.
Jul 17 '07 #2
rhx
14
big thanks epots9... one last thing, what if i would like to limit the number of textarea that can be added, where can i place the control?
Jul 17 '07 #3
epots9
1,351 Expert 1GB
big thanks epots9... one last thing, what if i would like to limit the number of textarea that can be added, where can i place the control?
a simple if statement can be used for that:
Expand|Select|Wrap|Line Numbers
  1. var count = 1;
  2. function addTextArea() 
  3. {
  4. if(count >= 5)
  5. {
  6. alert("too many");
  7. }
  8. else
  9. {
  10.     var newInput = document.createElement("textarea");
  11.     newInput.setAttribute("type", "text");
  12.     newInput.setAttribute('id','textarea' + count);
  13.     newInput.setAttribute('name','textarea' + count);
  14.     newInput.setAttribute("cols", "50");
  15.     newInput.setAttribute("row", "30");
  16.     document.getElementById("divToAddTextBox").appendChild(newInput);
  17. count++;
  18. }
  19. }
  20.  
good luck
Jul 17 '07 #4
rhx
14
that gives me something to start with, thanks a lot epots9.
Jul 17 '07 #5
epots9
1,351 Expert 1GB
np, i'm glad i could help

if u have anymore questions u can always come back to TSDN for support.
Jul 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
5
by: yawnmoth | last post by:
I'm having some difficulty with adding elements to a webpage via the DOM. The following works: main.htm: <script> js = document.createElement('script'); js.src='test.js';...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
2
by: Ken Fine | last post by:
In code, I'm adding javascript attributes to form elements on an ASP.NET page: body.Attributes.Add("onClick", "highlight(event);"); body.Attributes.Add("onKeyUp", "highlight(event);");...
18
omerbutt
by: omerbutt | last post by:
AJAX PROB WITH MULTIPLE RECORDS helo iam having problem in ma code will any body look out an help, i am trying t add sale record in the database and the checkthe quantity of the part slod and...
6
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.