943,960 Members | Top Members by Rank

Ad:
Aug 14th, 2007
0

Dynamic Javascript

Expand Post »
Hi guys,

I have a form with many text fields that are created from another program (java)
<input type=text name = 'myname1'>
<input type=text name = 'myname2'>
<input type=text name = 'myname3'>
.
.
.
.
.

and I have a javascript that loops through these text fields

for (var x = 1; x <= numberoftextfields; x++)
{
document.FormName.myname+x+.value = 'Test';

}

This javascript does not work.
Can somebody assist me on this.

Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
michael.ngobeni is offline Offline
20 posts
since Dec 2006
Aug 14th, 2007
0

Re: Dynamic Javascript

What script error do you receive? Try placing the myname variable names in another variable before setting the value.
Reputation Points: 25
Solved Threads: 18
Practically a Master Poster
binoj_daniel is offline Offline
645 posts
since Dec 2006
Aug 14th, 2007
0

Re: Dynamic Javascript

Use ID property from html element to call from javascript

HTML Syntax (Toggle Plain Text)
  1. <input type=text name = 'myname1' id='myname1'>
  2. <input type=text name = 'myname2' id='myname2'>
  3. <input type=text name = 'myname3' id='myname3'>
javascript Syntax (Toggle Plain Text)
  1. window.onload = function() {
  2. for (var i = 1; i < 4; i++) {
  3. document.getElementById('myname' + i).value = 'hi from for number ' + i;
  4. };
  5. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nandem is offline Offline
3 posts
since Aug 2007
Aug 14th, 2007
0

Re: Dynamic Javascript

Or something like this. Works in IE, FF and Opera.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type='text/javascript'>
  4. var CONST = 'myname';
  5. function doSomething(frm)
  6. {
  7. for(var i = 1; i < 4; ++i)
  8. {
  9. var txt = CONST + i;
  10. document.frm[txt].value = 'Test';
  11. }
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <form name="frm">
  17. <input type = 'text' name = 'myname1' /><br />
  18. <input type = 'text' name = 'myname2' /><br />
  19. <input type = 'text' name = 'myname3' /><br />
  20. <input type = 'button' onclick = 'doSomething(this.form);' value = 'Do' />
  21. </form>
  22. </body>
  23. </html>
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Aug 14th, 2007
0

Re: Dynamic Javascript

Hi guys,
document.FormName.myname+x+.value = 'Test';
The reason your code didn't work is that you were trying to construct the element variable as if it were a string... If you wanted to to it like this, you would need to construct the javascript you wanted to execute as a string and then execute it in an eval function...

i.e.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var strCommand = "document.FormName.myname" + x + ".value = 'Test';";
  2. eval(strCommand);

However the other solutions recommended are better practice.
Reputation Points: 20
Solved Threads: 5
Junior Poster
alpha_foobar is offline Offline
182 posts
since May 2005
Aug 16th, 2007
0

Re: Dynamic Javascript

I was off sick yesterday and I got to use your suggestions and they work perfect.
Thank you very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
michael.ngobeni is offline Offline
20 posts
since Dec 2006
Aug 16th, 2007
0

Re: Dynamic Javascript

document.FormName.myname+x+.value = 'Test';
replace with

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. if (document.getElementById('myname' + x)) {
  2. document.getElementById('myname' + x).value = 'Test';
  3. }


HMM WHY DID I NOT SEE EVERYONES POSTS 2 MINS AGO ?
DANIWEB THREW A BRAIN FART OR I AM A LEPER
Last edited by Fungus1487; Aug 16th, 2007 at 10:26 am. Reason: LEPRACY
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Aug 16th, 2007
0

Re: Dynamic Javascript

I'm surprised no-one thought of:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var textBoxArray = document.getElementsByTagName("input");
  2.  
  3. for(var i = 0; i < textBoxArray.length; ++i)
  4. {
  5. if(textBoxArray[i].type == "text")
  6. {
  7. textBoxArray[i].value = "test";
  8. }
  9. }

All previous solutions rely on:
a. Knowing how many inputs there are.
b. Knowing the naming convention.

Shoot me down if you like but to me they are code smells.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Aug 16th, 2007
0

Re: Dynamic Javascript

The previous ones are perfect because I have other input textboxes with names like myusname, mypassword, and so on.
So I only wanted textboxes with myname1, myneme2.......

These textboxes are created from an xml file

<person>
<myusername>....</myusername>
<mypassword>....</mypassword>
<myname1>....</myname1>
<myname2>....</myname2>
<myname3>....</myname3>
.
.
.
.
.
</person>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
michael.ngobeni is offline Offline
20 posts
since Dec 2006
Aug 16th, 2007
0

Re: Dynamic Javascript

Then that is best passed as a parameter IMO.

Just offering a more loosley coupled solution for the benefit of the thread that's all.

html 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" >
  3. <head>
  4. <title>Untitled Page</title>
  5. <script type="text/javascript">
  6. function setText(txtID, txtValue)
  7. {
  8. var textBoxArray = document.getElementsByTagName("input");
  9.  
  10. for(var i = 0; i < textBoxArray.length; ++i)
  11. {
  12. if(textBoxArray[i].type == "text" && textBoxArray[i].id.substring(0, txtID.length) == txtID)
  13. {
  14. textBoxArray[i].value = txtValue;
  15. }
  16. }
  17. }
  18. </script>
  19. </head>
  20. <body>
  21. <input type="text" id="myname1" /><br />
  22. <input type="text" id="myname2" /><br />
  23. <input type="text" id="myname3" /><br />
  24. <input type="button" onclick="setText('myname', 'test');" value = 'Go' />
  25. </body>
  26. </html>
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005

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: Javascript / Safari Problem - Please Help!!!
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Anybody have worked on a project like Google Adsense





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


Follow us on Twitter


© 2011 DaniWeb® LLC