I'm very new to Javascript although I have done a fair amount of PHP. The two languages seem to be sufficiently different or I'm doing something wrong to ask this question.
What is the best way to get the value of an option, then act on it according to the value in the option.
For example if I have
Apple
Banana
Orange
How do I get the script to do something with oranges?
Do I use Switch or a series of If/Elseif statements?
Also should I use the value tag for options as having spaces would cause issues wouldn't they?
Code:
<select>
<option value="fruitApple">Apples are nice</option>
<option value="fruitBanana">Bananas are nice</option>
<option value="fruitOrange">Oranges are nice</option>
</select>
If this is the case then how do you select the value?
the value is "fruitApple". The text is "Apples are nice". If you want to pass the text to the function that would be a different code.
Nothing actually changes on change - all that happens is that the function fires and the value gets passed to it. If you don't want that to happen, don't give it an onchange. As long as you give it a name and put it inside the form the value will be submitted when the form submits.
value is determined by the value attribute if set.
Always assign the value attribute or you'll get inconsistent results.
The problem I'm having is that I don't want the value changed on onchange but when the user clicks the Submit button.
Not quite sure what you mean by that, but you can use the onsubmit handler to read the value of the <select> and copy it presumably into a hidden-type element.
Bookmarks