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!

pass value to hidden field 1

Status
Not open for further replies.

petevick

Technical User
Jul 25, 2001
131
GB
I want to pass the value of a drop down list to a hidden field in my html code, something like...


<input type="hidden" name="quantity" value= list.value>


Any one got any ideas on how this might be achieved. I've had several goes, but all have failed

Pete Vickerstaff - Hedra Software
 
do you want to set it to the initial value of the drop down menu? have it change as the menu changes? more information please.

--Chessbot
 
Yes, it was a bit of a sketchy question. When the user clicks the submit button, I'd like the value to be set to the current value of the drop down list.

Pete Vickerstaff - Hedra Software
 
Code:
function setValue()
{
    window.document.myform.myhidden.value=window.document.myform.myselect[selectedIndex].value;
}

--Chessbot
 
sorry, wrong code

Code:
function setValue()
{
   var form=window.document.myform;
   var select=form.myselect;
   form.myhidden.value=select[select.selectedIndex].value;
   alert(form.myhidden.value);
}

and
Code:
<form name="myform" onSubmit="setValue()">
<input type="hidden" name="myhidden">
<select name="myselect">
<option value="apples" selected>Apples
<option value="pears">Pears
<option value="plums">Plums
</select>
<imput type="submit" value="Submit">
</form>

--Chessbot
 
Thanks chessbot, thats spot on. I would have liked the select outside of the form tags, but that does not work, but I can work around the select inside. Have a star.

Pete Vickerstaff - Hedra Software
 
<select> is a form element so it must reside inside a form. If you have it somewhere completely elsewhere on the page, you can have two separate forms, as you can see the supplied code references the form id/name. Why exactly you wouldn't want to put the select tag into a form, I have no idea. If you tell us, maybe we can suggest on a proper solution.
 
Hi Vragabond, the select tag is fine inside the form, just a slight bit of redesign of my page and the supplied code is excellent. I am still learning about all the facets of html, but learning very fast thanks to excellent resources like Tek-Tips.

Pete Vickerstaff - Hedra Software
 
There's a slight problem with this approach, some visitors may have Javascript switched off when they browse. Not many - I admit - but why chance it?

Anyway, what's the point of copying the value of one field into another in the same form? If you're sending them both to a script, why not send it once and let the script do it?

-- Chris Hunt
 
Does this script use data from the drop-down as well, or just from the hidden field?

When you submit a form, it submits 2 things: the name of each form element, and it's value. It doesn't care if the form element is a drop-down, input, radio button, whatever. The exception is inputs of type=image, which submit the x and y co-ordinates when they're clicked.

If this script is only using the value of the hidden field, then all you need to do is remove the hidden field and rename the drop-down.

<marc> i wonder what will happen if i press this...[ul][li]please tell us if our suggestion has helped[/li][li]need some help? faq581-3339[/li][/ul]
 
Hi manarth, thats a good question, I don't see why that wouldn't work. I don't have access to the script, but I guess I could try it. The page is actually working fine now, so maybe its a case of if it ain't broke, don't fix it, then again, now you've put the idea there.... damn, I'll have to try...

<Pete> if it ain't broke, break it...

Pete Vickerstaff - Hedra Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top