Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

howto access form key value?

Status
Not open for further replies.

Candyb

Technical User
Oct 2, 2001
12
CA
Hi,

if i had a form name='list':
<input name='box' type='text'>blah</input>
<input type='submit' name='hello' value='3' >
i know that you grab the value by
document.list.box.value = blah
but what if you want to access hello?
document.list.key? or document.list.key(2) is wrong...
my form only has two elements, a text and a submit.
my submit's name is built dynamically. (aside from the point)
I have many submit buttons but the form only has 2 elements.

Is it possible to grab that name?

Thanks,
Candy
 
Huh?

How is the name built dynamically?

And normally your referenced elements ought to have an ID property... which I think most browsers will default to the same value as the name but it isn't trustworthy.

You ought to have something like:

<form id='list' name='list'>
<input id='box' name='box' type='text'>blah</input>
<input id='hello' name='hello' type='submit' value='1'>
<input id='hello' name='hello' type='submit' value='2'>
<input id='hello' name='hello' type='submit' value='3'>
</form>

You normally reference the ID in script. This eliminates most possibilities for confusion.

So you have things like:

document.all.list.hello(2).value = x

For example:

<html>
<head></head>
<body onload=&quot;document.all.list.hello(2).value='Hey!'&quot;>
<form id='list' name='list'>
<input id='box' name='box' type='text' value='blah'>
<input id='hello' name='hello' type='submit' value='1'>
<input id='hello' name='hello' type='submit' value='2'>
<input id='hello' name='hello' type='submit' value='3'>
</form>
</body>
</html>

Usually you can abbreviate a bit:

<body onload=&quot;list.hello(2).value='Hey!'&quot;>

By reusing the name value multiple times you create a collection, which can be indexed (base 0, run the above example and the 3rd button gets &quot;Hey!&quot; - NOT the 2nd one!).

Also, <input type=text> is NOT a block tag. You must be thinking of <textarea> ? There is no </input>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top