| | |
Dynamic Javascript
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2006
Posts: 20
Reputation:
Solved Threads: 0
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
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
What script error do you receive? Try placing the myname variable names in another variable before setting the value.
•
•
Join Date: Aug 2007
Posts: 3
Reputation:
Solved Threads: 0
Use ID property from html element to call from javascript
HTML Syntax (Toggle Plain Text)
<input type=text name = 'myname1' id='myname1'> <input type=text name = 'myname2' id='myname2'> <input type=text name = 'myname3' id='myname3'>
javascript Syntax (Toggle Plain Text)
window.onload = function() { for (var i = 1; i < 4; i++) { document.getElementById('myname' + i).value = 'hi from for number ' + i; }; }
Or something like this. Works in IE, FF and Opera.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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.
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.
However the other solutions recommended are better practice.
i.e.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var strCommand = "document.FormName.myname" + x + ".value = 'Test';"; eval(strCommand);
However the other solutions recommended are better practice.
replace with
HMM WHY DID I NOT SEE EVERYONES POSTS 2 MINS AGO ?
DANIWEB THREW A BRAIN FART OR I AM A LEPER
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
I'm surprised no-one thought of:
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2006
Posts: 20
Reputation:
Solved Threads: 0
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>
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>
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.
Just offering a more loosley coupled solution for the benefit of the thread that's all.
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <script type="text/javascript"> function setText(txtID, txtValue) { var textBoxArray = document.getElementsByTagName("input"); for(var i = 0; i < textBoxArray.length; ++i) { if(textBoxArray[i].type == "text" && textBoxArray[i].id.substring(0, txtID.length) == txtID) { textBoxArray[i].value = txtValue; } } } </script> </head> <body> <input type="text" id="myname1" /><br /> <input type="text" id="myname2" /><br /> <input type="text" id="myname3" /><br /> <input type="button" onclick="setText('myname', 'test');" value = 'Go' /> </body> </html>
![]() |
Similar Threads
- Using "variable variables" in Javascript (JavaScript / DHTML / AJAX)
- Dynamic pull out menus (JavaScript / DHTML / AJAX)
- Creating Images dynamically in Javascript (ASP.NET)
- Making T.Greer's JavaScript-synthesised combobox dynamic (JavaScript / DHTML / AJAX)
- Need help in design and javascript code for Dynamic form (JavaScript / DHTML / AJAX)
- Urgent.....Dynamic Changes.... (JavaScript / DHTML / AJAX)
- Help: Only last loaded javascript runs (JavaScript / DHTML / AJAX)
- using variable for dynamic from elements (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Javascript / Safari Problem - Please Help!!!
- Next Thread: Anybody have worked on a project like Google Adsense
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor date debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n






