473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi all,
I have a Javascript variable that contains the name of a form input-
input_name = "document.myfor m.ip"
How can I get the value of this form input, "ip" using the variable
input_name?
input_name.valu e 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
6 1880
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.myfor m.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.myfor m.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.getEl ementById) {
input_name=docu ment.getElement ById("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.valu e 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
ne*****@gmail.c om wrote:
I have a Javascript variable that contains the name of a form input-
input_name = "document.myfor m.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
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.getEl ementById) {
input_name=docu ment.getElement ById("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
"Matt Kruse" <ne********@mat tkruse.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(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.matc h(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 8 '06 #5
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 setNestedProper ty(El, propName, myValue){
var propertyNameAsL ist = propName.split( '.');
var myProp = El;
for (var i=0; i < propertyNameAsL ist.length; i++){
if (i == (propertyNameAs List.length - 1)){
myProp[ propertyNameAsL ist[i] ] = myValue;
return;
}
myProp = myProp[ propertyNameAsL ist[i] ];
}
}

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

Lasse Reichstein Nielsen wrote:
"Matt Kruse" <ne********@mat tkruse.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(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.matc h(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'


Jun 8 '06 #6
Erm, I guess it would be nicer if I actually posted code that solved
the problem everyone was discussing in the first place :)

function getNestedProper tyValue(El, propName){
var propertyNameAsL ist = propName.split( '.');
var myProp = El;
for (var i=0; i < propertyNameAsL ist.length; i++){
if (i == (propertyNameAs List.length - 1)){
return myProp[ propertyNameAsL ist[i] ] ;
}
myProp = myProp[ propertyNameAsL ist[i] ];
}
}

var fieldValue = getNestedProper tyValue(documen t,
'forms.postform .textbox.value' );
var formsList = getNestedProper tyValue(documen t, '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 setNestedProper ty(El, propName, myValue){
var propertyNameAsL ist = propName.split( '.');
var myProp = El;
for (var i=0; i < propertyNameAsL ist.length; i++){
if (i == (propertyNameAs List.length - 1)){
myProp[ propertyNameAsL ist[i] ] = myValue;
return;
}
myProp = myProp[ propertyNameAsL ist[i] ];
}
}

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

Lasse Reichstein Nielsen wrote:
"Matt Kruse" <ne********@mat tkruse.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(?:\.f orms)?\.([$_a-zA-Z][$\w]*)(?:\.elements )?\.([$_a-zA-Z][$\w]*)$/;
// ...
var match = input_name.matc h(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'


Jun 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2811
by: NewbieJon | last post by:
I am attempting to send the variable "sComputerName" from my ActiveX script to "GetInfo.asp" using javascript. (Having been advised this is the way to get my ActiveX variable into my ASP script) My code is below. I am getting the following error "MyForm.oNetwork.Value is Null or is not an object". I'm struggling to diagnose what to do next. ...
16
11468
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base =...
1
3374
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with ***********************, I've included all the code incase that isn't where the problem is. Any help would be hugely appreciated. Mick
6
9366
by: paul | last post by:
HI! How do we send a variable from an Iframe page back to its parent? I have a script that calculates the iframe's window size but I need to know how to send that value back to its parent so I can use it there. Thanks in advance :) Paul
5
6834
by: james.calhoun | last post by:
I feel like this should be really easy... I want a hidden field in a form to have its value defined when someone clicks on a link. So if they click on link "A" the value of the hidden field becomes "A", while if they click "B" the value of hidden field becomes "B" Help?
4
1815
by: frank78 | last post by:
Hi all, I'm having an issue with passing form values from one page to the next. I wrote a function that I use to change the value of a hidden field to any number of dynamically generated form select value. The function "works" in the sense that when I alert, the same values occur. This tells me some value is being set to the id =...
1
3290
by: satish2112 | last post by:
Hi, I have a text-area which contains values from mysql database and 2 buttons, Edit and Update. When I click on the Edit button, I can edit the text-area (initially non-editable). After this, if I click on the Update button, the values in the text-area must be updated in the mysql database. I am storing the values of the text-area in a...
1
1928
by: macintoshhondo | last post by:
Hi ! i am a newbie and dont know javascript much. what i really need is a simple javascript code that can insert number in the value section of the different forms from the one form. FORM 1: <form> <input type="text" name="textField" size="20"><br> </form> In the "TextField " of Form 1 i type any number and let the number be 100. Now...
4
4864
by: Michael Munch | last post by:
Hi I want to read the value of af text-field, create dynamic, in a form. Se below a small test-site to do that (but readning fails): I use the function Test_Read for reading the value from the dynamic create text-field "txtName". I thanks...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.