By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,639 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,639 IT Pros & Developers. It's quick & easy.

How can I get the value of a form input whose name is in a Javascript variable?

P: n/a
Hi all,
I have a Javascript variable that contains the name of a form input-
input_name = "document.myform.ip"
How can I get the value of this form input, "ip" using the variable
input_name?
input_name.value returns undefined value and input_name + ".value"
returns the string document.myform.ip.value
Any help will be highly appreciated.

Thanks,
Neelay.

Jun 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Wed, 07 Jun 2006 17:36:04 -0700, neelay1 wrote:
Hi all,
I have a Javascript variable that contains the name of a form input-
input_name = "document.myform.ip"
Is this the actual code? If so you've made a mistake - all you've done is
set the variable input_name equal to the /string/ "document.myform.id"
this has nothing to do with the object document.myform.id. Also, I think
this syntax only works in internet explorer - you're better using
something like:

if (document.getElementById) {
input_name=document.getElementById("ip");
}

and setting the 'id' property of the form input to 'ip'
How can I get the value of this form input, "ip" using the variable
input_name?
input_name.value returns undefined value and input_name + ".value"
returns the string document.myform.ip.value
Any help will be highly appreciated.

Thanks,
Neelay.


Jun 8 '06 #2

P: n/a
ne*****@gmail.com wrote:
I have a Javascript variable that contains the name of a form input-
input_name = "document.myform.ip"
First, reference forms correctly:
http://www.javascripttoolbox.com/bes.../new.php#forms

document.myform is not a good idea.
How can I get the value of this form input, "ip" using the variable
input_name?


Second, instead of your code, do:

var form_name = "myform";
var input_name = "ip";

then

var value=document.forms[form_name].elements[input_name].value;

See http://www.javascripttoolbox.com/bes...#squarebracket

If there is absolutely no way to avoid having

input_name = "document.forms.myform.ip";

then you would need to use eval:

eval("var value = "+input_name+".value");

But this is discouraged:
http://www.javascripttoolbox.com/bes...s/new.php#eval

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 8 '06 #3

P: n/a
andy baxter wrote:
<snip>
document.myform.id. Also, I think this syntax only works in
internet explorer - you're better using something like:

if (document.getElementById) {
input_name=document.getElementById("ip");
}

and setting the 'id' property of the form input to 'ip'

<snip>

The 'shortcut' form accessor properties are widely (apparently
universally) supported in HTML DOMs. The normal recommended method is to
access form controls through the W3C standard - document.forms -
collection and then through the - elements - collection of the form
element (that approach being back-compatible with 'shortcut' supporting
browsers, W3C DOM standard and available in XHTML DOMs):-

<URL: http://jibbering.com/faq/faq_notes/form_access.html >

Richard.
Jun 8 '06 #4

P: n/a
"Matt Kruse" <ne********@mattkruse.com> writes:
If there is absolutely no way to avoid having

input_name = "document.forms.myform.ip";

then you would need to use eval:

eval("var value = "+input_name+".value");
"Need" is such a strong word :)

You can do it like that, and it's probably also the simplest
way, but it has absolutely no validation of the format of the
input string.

You could parse the string, something like:
---
// tests and captures: document[.forms].<formid>[.elements].<controlname>
var formRE = /^document(?:\.forms)?\.([$_a-zA-Z][$\w]*)(?:\.elements)?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.match(formRE);
if (formRE) {
var value = document.forms[match[1]].elements[match[2]];
}
---
Then you both test the input and avoid eval.
But this is discouraged:
http://www.javascripttoolbox.com/bes...s/new.php#eval


And mostly unnecessary.
There are *very* few things that can't be done without eval.
There are almost as few that is best done with eval.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 8 '06 #5

P: n/a
Here is an alternative way to parse property names. This method
doesn't validate the input, but it is generalized, and handles any
property name that gets passed to it.

function setNestedProperty(El, propName, myValue){
var propertyNameAsList = propName.split('.');
var myProp = El;
for (var i=0; i < propertyNameAsList.length; i++){
if (i == (propertyNameAsList.length - 1)){
myProp[ propertyNameAsList[i] ] = myValue;
return;
}
myProp = myProp[ propertyNameAsList[i] ];
}
}

setNestedProperty(myDiv, 'style.display', 'block');
setNestedProperty(myDiv, 'className', 'foo');

Lasse Reichstein Nielsen wrote:
"Matt Kruse" <ne********@mattkruse.com> writes:
If there is absolutely no way to avoid having

input_name = "document.forms.myform.ip";

then you would need to use eval:

eval("var value = "+input_name+".value");


"Need" is such a strong word :)

You can do it like that, and it's probably also the simplest
way, but it has absolutely no validation of the format of the
input string.

You could parse the string, something like:
---
// tests and captures: document[.forms].<formid>[.elements].<controlname>
var formRE = /^document(?:\.forms)?\.([$_a-zA-Z][$\w]*)(?:\.elements)?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.match(formRE);
if (formRE) {
var value = document.forms[match[1]].elements[match[2]];
}
---
Then you both test the input and avoid eval.
But this is discouraged:
http://www.javascripttoolbox.com/bes...s/new.php#eval


And mostly unnecessary.
There are *very* few things that can't be done without eval.
There are almost as few that is best done with eval.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Jun 8 '06 #6

P: n/a
Erm, I guess it would be nicer if I actually posted code that solved
the problem everyone was discussing in the first place :)

function getNestedPropertyValue(El, propName){
var propertyNameAsList = propName.split('.');
var myProp = El;
for (var i=0; i < propertyNameAsList.length; i++){
if (i == (propertyNameAsList.length - 1)){
return myProp[ propertyNameAsList[i] ] ;
}
myProp = myProp[ propertyNameAsList[i] ];
}
}

var fieldValue = getNestedPropertyValue(document,
'forms.postform.textbox.value');
var formsList = getNestedPropertyValue(document, 'forms');
Noah Sussman wrote:
Here is an alternative way to parse property names. This method
doesn't validate the input, but it is generalized, and handles any
property name that gets passed to it.

function setNestedProperty(El, propName, myValue){
var propertyNameAsList = propName.split('.');
var myProp = El;
for (var i=0; i < propertyNameAsList.length; i++){
if (i == (propertyNameAsList.length - 1)){
myProp[ propertyNameAsList[i] ] = myValue;
return;
}
myProp = myProp[ propertyNameAsList[i] ];
}
}

setNestedProperty(myDiv, 'style.display', 'block');
setNestedProperty(myDiv, 'className', 'foo');

Lasse Reichstein Nielsen wrote:
"Matt Kruse" <ne********@mattkruse.com> writes:
If there is absolutely no way to avoid having

input_name = "document.forms.myform.ip";

then you would need to use eval:

eval("var value = "+input_name+".value");


"Need" is such a strong word :)

You can do it like that, and it's probably also the simplest
way, but it has absolutely no validation of the format of the
input string.

You could parse the string, something like:
---
// tests and captures: document[.forms].<formid>[.elements].<controlname>
var formRE = /^document(?:\.forms)?\.([$_a-zA-Z][$\w]*)(?:\.elements)?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.match(formRE);
if (formRE) {
var value = document.forms[match[1]].elements[match[2]];
}
---
Then you both test the input and avoid eval.
But this is discouraged:
http://www.javascripttoolbox.com/bes...s/new.php#eval


And mostly unnecessary.
There are *very* few things that can't be done without eval.
There are almost as few that is best done with eval.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Jun 8 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.