problems referencing form elements by deriving their name from the value of another element

L

libsfan01

Hi all whats wrong with this script?

i have named a form text box dynamically using php each row has a
textbox called q101, q102 ... etc and i want to make sure my users dont
input a quantity greater than the value of that box. to know which box
applies to which of the products i have used a radio button which
contains the id of the product which corresponds to the name of the box
when a q is added to the front (javascript seems not to like forn names
that are just numbers).

what im trying to do is get the user to click the radio button submit
the form and have the id of the radio button passed as a variable along
with the letter q to the javascript function which uses this string to
locate the right quantity text box to tell the user if he has selected
too many of the product in question.

javascript seems to have a problem in taking the value of a text box
adding it to a string and using this new combined string value to
reference the name of another form element and check its value.

when i use a constant e.g. q191 instead of the var quantityvariation
(in maxquantity declaration) it works fine, maxquantity is the value of
the textbox that contains the value for quantity.

Any idea how i can get this to work?


<script language="javascript" type="text/javascript">
function validForm(product) {

var variationid = product.order.value;
var quantityvariation = "q" + variationid;
var maxquantity = product.quantityvariation.value;

//0 < 1 is just there for testing

if (0 < 1) {
alert(maxquantity)
product.quantity.focus()
return false
}

}
</script>
 
L

Lee

libsfan01 said:
javascript seems to have a problem in taking the value of a text box
adding it to a string and using this new combined string value to
reference the name of another form element and check its value.

References of the form: formName.elementName.value
are not strings, and may not contain string variables.
There is another notation that you can use when part of the
reference path is a variable. In your case, since the variable
part is the name of an element of the form referenced as "product",
you would use "bracket notation":

product.elements[quantityvariation].value

There is a shortcut notation that carries the risks and advantages
of most shortcuts:

product[quantityvariation].value

http://www.jibbering.com/faq/#FAQ4_13


--
 
L

libsfan01

cheers jim

tried that but it javascript tells me that maxquantity is undefined
when i print its value

Hi all whats wrong with this script?

i have named a form text box dynamically using php each row has a
textbox called q101, q102 ... etc and i want to make sure my users dont
input a quantity greater than the value of that box. to know which box
applies to which of the products i have used a radio button which
contains the id of the product which corresponds to the name of the box
when a q is added to the front (javascript seems not to like forn names
that are just numbers).

what im trying to do is get the user to click the radio button submit
the form and have the id of the radio button passed as a variable along
with the letter q to the javascript function which uses this string to
locate the right quantity text box to tell the user if he has selected
too many of the product in question.

javascript seems to have a problem in taking the value of a text box
adding it to a string and using this new combined string value to
reference the name of another form element and check its value.

when i use a constant e.g. q191 instead of the var quantityvariation
(in maxquantity declaration) it works fine, maxquantity is the value of
the textbox that contains the value for quantity.

Any idea how i can get this to work?


<script language="javascript" type="text/javascript">
function validForm(product) {

var variationid = product.order.value;
var quantityvariation = "q" + variationid;
var maxquantity = product.quantityvariation.value;

var maxquantity = product[quantityvariation];
//0 < 1 is just there for testing

if (0 < 1) {
alert(maxquantity)
product.quantity.focus()
return false
}

}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top