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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

making a hidden field take the value of a select box

Status
Not open for further replies.

fiuPikeOY

Programmer
Jun 14, 2004
143
US
Hello,

I need the subject from a mail form to be = to the value selected from a select box. I have the following..

onSubmit( I call the javascript)

javascript...

funcion emailSubject{
document.mailForm.subject.value = document.mailForm.SelectboxName;
}

But it's not working. How do I reference the value from the selectBox. If I say:
document.mailForm.subject.value = "test" it works. I just need to equal it to select Box.

Help?

Thanks
 
Code:
if (document.mailForm.SelectboxName.checked) {
   document.mailForm.subject.value = document.mailForm.SelectboxName.value;
}
 
Code:
var f = document.forms['mailForm'];
var sub = f.elements['subject'];
var sel = f.elements['SelectboxName'];
sub.value = sel.options[sel.selectedIndex].value;

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
rac2, I tried

document.mailForm.subject.value = document.mailForm.SelectboxName.value;

and it didn't work. Without the "check" issue of course

CLflaVa, can you explain that a bit more. I don't understand it.

Thanks in advance
 
fiuPikeOY,

Your request can be done in one line, I just broke it down for you so you could see what I was doing:

Code:
// get the form object
var f = document.forms['mailForm'];

// get the subject field
var sub = f.elements['subject'];

// get the select box
var sel = f.elements['SelectboxName'];

// make the subject field's value be the select box's value
sub.value = sel.options[sel.selectedIndex].value;

The one-liner would be:

Code:
document.forms['mailForm'].elements['subject'].value = document.forms['mailForm'].elements['SelectboxName'].options[document.forms['mailForm'].elements['SelectboxName'].selectedIndex].value;

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
this worked perfect. Thanks guys. You're awesome
 
Oh boy, what a gaff! I suppose the value would be undefined.

In my defense, I thought maybe the question was regarding a checkbox. I mean 'select box' could be a select element or a checkbox type of input element. Couldnt it?
 

I mean 'select box' could be a select element or a checkbox type of input element. Couldnt it?

Not really, no.

Dan


The answers you get are only as good as the information you give!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top