Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Apologies for so basic a question, but I can't find an answer anywhere. When the button is clicked, I need to change the value a select passes to the next page. I'm not trying to change the option list on this page, but rather trying to change the value which the select passes to the next page.

<form name="myform" action="test2.php" method="get">
<select name="selectname" id="selectid">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
    <input type="submit" onclick="document.getElementById('selectid').value='3';" />
</form>

I could easily change the passed value if it were an input rather than a select, but for some reason I am unable to get this to work with a select. Either a straight javascript or a prototype answer will work.

share|improve this question
    
I don't think you can set the value like that. Your best bet would be to change the value of the option that is selected –  JohnP May 2 '11 at 4:36

1 Answer 1

up vote 2 down vote accepted

There is no option with the value 3 so document.getElementById('selectid').value='3'; will have no effect.

Setting the value for a <select> way simply selects the option with the matching value. If that value doesn't exist, nothing happens. Is that what you're trying to do?


Edit

If what @JohnP suggested in his comment is what you're trying to achieve i.e. change the value attribute for an <option>, you could do this in a function that is invoked on click:

var s = document.getElementById('test'); //get the select element
var o = s.options[s.selectedIndex]; //get the option element that is selected
o.value='3'; //change it's value
//o.innerHTML = '3'; //optionally change the text displayed
share|improve this answer
    
Ah, makes perfect sense. Thanks. –  user733902 May 2 '11 at 4:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.