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!

Assigning a value to a form field through javascript

Status
Not open for further replies.

Idee

Programmer
Jul 8, 2003
51
NZ
Hi all

I have a form with a select box with size options in it and there is another select box for the user to say if he wants to his own custom sizes. If user selects yes from this second select box, two text fields should be displayed for the user to input his own values of width and height.

I submit the form using javascript and calculate the custom width and height combination and put this value in the form field which holds the size value.

My javascript code looks like this:

function fmsubmit(){
document.fm.action='index.cfm';
var size = document.fm.optionsSize.value;
var cwidth = document.fm.cwidth.value;
var cheight = document.fm.cheight.value;
var finalval = cwidth + ' X ' + cheight;
document.fm.optionsSize.value = finalval;
fm.submit;
}

New finalval value doesn't get assigned to document.fm.optionsSize.value

Does it happen because an option is also selected from the select box with name as optionsSize which has a value?

Any help is appreciated. Thanks

 
Is the select box also named optionsSize? Is there a text box names optionsSize?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
I'm not exactly sure what you're getting at, but the appropriate way of finding out what value is selected in a select box is below:

Code:
var theSel = document.forms['fm'].elements['optionsSize'];
var theSelVal = theSel.options[theSel.selectedIndex].value;

Also, may I suggest using radio buttons instead of the yes/no drop down?

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Thanks for your replies.

I have just one select box with the name optionsSize and no text box. What I am trying to do is changing the value passed for optionsSize if user gives his own sizes for width and height and using this width and height, I assign new value for optionsSize through javascript. optionsSize select box will have a different value from what I assign through javascript before submitting the form.

Do you think I have made it clear enough now?

Thanks a lot.
 
don't change a value of the selectbox. Use a hidden field. Call a function from the onsubmit event. If the select box's selectedIndex is 0 (first option is selected) then assign the hidden field the value of the text box. If it's greater than 0, assign the hidden field the appropriate value of the select box.

Then, in your next page, refer to the hidden field.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Code:
<form name=myForm>
<select id=optionsSize>
<option value="1x2">1x2
<option value="2x4">2x4
</select>
<input name=val1> X <input name=val2>
<input type=button value="add" onClick="addSize()">
</form>

<script>
 function addSize(){
   newVal = document.myForm.val1.value + "x" + document.myForm.val2.value
   theSel = document.getElementById("optionsSize")
   theSel.options[theSel.options.length] = new Option(newVal, newVal)
   theSel.selectedIndex = theSel.options.length -1
 }
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Thanks cLFlaVA for the prompt reply.

My problem here is that I can not use another form field. I have to use the same form field as that is the field which gets processed in all next files and I am not in a position to change all those files.

Do you think I should be able to reassign the value to selectbox field using javascript? I always thoght that it should not ba any problem but it didn't work.

Thanks

 
how about renaming the select box then? and then making the hidden field's name "optionsSize"?

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top