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

Assigning a value to Hidden Type.

P: 68
Hi, I am a beginner here.

I was not knowing how to assign the value of a Dropdown menu to a Hidden type variable.

Expand|Select|Wrap|Line Numbers
  1. <form name=form1 onsubmit="test()">
  3. <select name=car>
  4.   <option value ="volvo">Volvo</option>
  5.   <option value ="saab">Saab</option>
  6.   <option value ="opel">Opel</option>
  7.   <option value ="audi">Audi</option>
  8. </select>
  10. <input type=hidden name=hidden_car value="**********">

now, i want the hidden variable 'hidden_car" TO have the value that is selected from the dropdown menu before the SUBMIT button is hit.
in the above example, im not sure what shud be entered in place of ******** for the hidden variable.

Any help is appreciated.

Thank You
Oct 13 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 5,390
hi ...

please use CODE tags when posting source-code ...

to your problem: you may use the onchange-event of the select-element to invoke a function that sets its current value to the value of the hidden field, here is an example (snippet):

Expand|Select|Wrap|Line Numbers
  1. function set_value(obj) {
  2.     // give your hidden field an id, so that we may retrieve it here
  3.     var hidden_field = document.getElementById('my_hidden_field');
  5.     hidden_field.value = obj.value;
  6. }
  8. <select name="car" onchange="set_value(this);">
give this a try and post back in case you encounter problems with it. note that you should assign a default value to the field ... since the function is invoked onchange ... that means only when another option than the current option will be selected ...

kind regards

ps: please enclose html-nodes attribute values in double-quotes in your code!
Oct 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.