943,169 Members | Top Members by Rank

Ad:
Aug 31st, 2010
0

How to call dynamically created textboxes

Expand Post »
Please help! I have a form with checkboxes. When one or more checkboxes are clicked, I want the program to dynamically create a div, assign that div an editable text area, then when user clicks submit, I need it to gather the input from EACH textbox and output that to another textbox. I can't figure out what I'm doing wrong. It always tells me "Object Expected" on clicking the checkboxes. If I have it completely wrong, Please bear with me - I'm still learning!


JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <head>
  4. <title>Text Formatter</title>
  5. <script type="text/javascript">
  6. <!--
  7.  
  8. function checkIt()
  9. {
  10. // Gather dynamically created textbox based on id and assign output to textstring
  11. for (i=0;i<5;i++) {
  12. var box1=document.getElementById('mytext'[i])
  13. if (box1.value){
  14. textstring += box1.value + '\n';
  15. }
  16. }
  17. document.forms['example'].output.value = textstring;
  18. }
  19. function changeIt()
  20. {
  21. //Create textbox within div 'appndiv' and add number to the name based on which box
  22. //selected it
  23.  
  24. for (var i=0;i<document.forms['example'].ipinfo.length;i++) {
  25. if (document.forms['example'].ipinfo[i].checked) {
  26. var newdiv = document.forms['example'].createElement('div');
  27. var mydiv = "my_div" + [i];
  28. newdiv.id=mydiv;
  29. newdiv.innerHTML = '<br>Please copy and paste additional information here<input type='textarea' id=''mytext' +i')>';
  30. document.getElementById('appndiv').appendChild(newdiv);
  31. }
  32. }
  33. }
  34. // -->
  35. </script>
  36. </head>
  37. <body vlink="blue">
  38.  
  39.  
  40. <h2>Text Formatter</h2>
  41.  
  42. <form name="example">
  43. <table class="form">
  44. <tr>
  45. <td>IP information Gathered From:</td>
  46. <td><input type="checkbox" name="ipinfo"/>Portal<br />
  47. <input type="checkbox" name="ipinfo" onclick="changeIt()" />Ping<br />
  48. <input type="checkbox" name="ipinfo" onclick="changeIt()" />Trace Route<br />
  49. <input type="checkbox" name="ipinfo" onclick="changeIt()" />Whois Lookup<br />
  50. <div id="appndiv"></div>
  51. </tr>
  52. <tr><tr><td><br><br></td></td></tr>
  53. <tr><td colspan="2"><input type="button" action="#" onclick="checkit(); return false" value="Submit form" />
  54. <tr><tr><td><br><br></td></td></tr>
  55. <tr><td colspan="2"><textarea cols="50" rows="25" name="output">When you hit 'Submit' the user input will be written to this text area</textarea></td></tr>
  56. </table>
  57. </form>
  58. </body>
  59. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thinstaafl is offline Offline
1 posts
since Aug 2010
Sep 3rd, 2010
0
Re: How to call dynamically created textboxes
First in the code posted above there are syntax errors at line 29. Since you are assiging a string to a variable and that string has single quotes you have to escape them correctly as follows,
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. newdiv.innerHTML = '<br>Please copy and paste additional information here<input type=\'textarea\' id=\'mytext\''+i+'\')>';

Now on line 26 you are trying to create a new element as
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var newdiv = document.forms['example'].createElement('div');

But you cannot create a new element on form element. You will have to create a new element on document and than append it as a child to form element as follows,
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var newdiv = document.createElement('div');
  2. document.forms['example'].appendChild(newdiv);

Hope this helps
Sponsor
Reputation Points: 26
Solved Threads: 41
Junior Poster
parry_kulk is offline Offline
166 posts
since Jan 2007
Sep 28th, 2010
0
Re: How to call dynamically created textboxes
Translation.pk have expert translators for many languages including

Arabic translation, English Translation, check republic, French

translation, German Translation, Italian Translation, Japanese

Translation, Russian Translation, Spanish Translation, Dutch

Translation, Greek Translation, Hindi Translation, Korean Translation,

Polish Translation, Portuguese Translation and in many more languages.

I found best Arabic translators at

http://www.translation.pk/arabic-translation.html

Regards,
SANA SALEEM-16628
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sana3366 is offline Offline
1 posts
since Sep 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Dropdown search script...
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: how to make td content editable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC