User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,476 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,780 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 784 | Replies: 9
Reply
Join Date: Dec 2006
Posts: 20
Reputation: michael.ngobeni is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
michael.ngobeni michael.ngobeni is offline Offline
Newbie Poster

Dynamic Javascript

  #1  
Aug 14th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: United States
Posts: 613
Reputation: binoj_daniel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 15
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Dynamic Javascript

  #2  
Aug 14th, 2007
What script error do you receive? Try placing the myname variable names in another variable before setting the value.
Reply With Quote  
Join Date: Aug 2007
Posts: 3
Reputation: Nandem is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Nandem Nandem is offline Offline
Newbie Poster

Re: Dynamic Javascript

  #3  
Aug 14th, 2007
Use ID property from html element to call from javascript

  1. <input type=text name = 'myname1' id='myname1'>
  2. <input type=text name = 'myname2' id='myname2'>
  3. <input type=text name = 'myname3' id='myname3'>
  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. }
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Dynamic Javascript

  #4  
Aug 14th, 2007
Or something like this. Works in IE, FF and Opera.

<html>
<head>
    <script type='text/javascript'>
    var CONST = 'myname';
    function doSomething(frm)
    {
        for(var i = 1; i < 4; ++i)
        {
            var txt = CONST + i;
            document.frm[txt].value = 'Test';
        }
    }
    </script>
</head>
<body>
    <form name="frm">
        <input type = 'text' name = 'myname1' /><br />
        <input type = 'text' name = 'myname2' /><br />
        <input type = 'text' name = 'myname3' /><br />
        <input type = 'button' onclick = 'doSomething(this.form);' value = 'Do' />
    </form>
</body>
</html>
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: May 2005
Location: Wellington, New Zealand
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: Dynamic Javascript

  #5  
Aug 14th, 2007
Originally Posted by michael.ngobeni View Post
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.
var strCommand = "document.FormName.myname" + x + ".value = 'Test';";
eval(strCommand);

However the other solutions recommended are better practice.
Reply With Quote  
Join Date: Dec 2006
Posts: 20
Reputation: michael.ngobeni is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
michael.ngobeni michael.ngobeni is offline Offline
Newbie Poster

Re: Dynamic Javascript

  #6  
Aug 16th, 2007
I was off sick yesterday and I got to use your suggestions and they work perfect.
Thank you very much.
Reply With Quote  
Join Date: Apr 2007
Location: Birmingham
Posts: 378
Reputation: Fungus1487 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 38
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Whiz

Re: Dynamic Javascript

  #7  
Aug 16th, 2007
Originally Posted by michael.ngobeni View Post
document.FormName.myname+x+.value = 'Test';


replace with

if (document.getElementById('myname' + x)) {
   document.getElementById('myname' + x).value = 'Test';
}


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
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Dynamic Javascript

  #8  
Aug 16th, 2007
I'm surprised no-one thought of:

var textBoxArray = document.getElementsByTagName("input");

for(var i = 0; i < textBoxArray.length; ++i)
{
    if(textBoxArray[i].type == "text")
    {
        textBoxArray[i].value = "test";
    }
}

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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Dec 2006
Posts: 20
Reputation: michael.ngobeni is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
michael.ngobeni michael.ngobeni is offline Offline
Newbie Poster

Re: Dynamic Javascript

  #9  
Aug 16th, 2007
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>
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,166
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 59
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Dynamic Javascript

  #10  
Aug 16th, 2007
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.

  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>
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 2:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC