Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  146] [ 2]  / answers: 1 / hits: 26283  / 12 Years ago, tue, june 12, 2012, 12:00:00

I want this script to add an option to the list.



When you open the list I want the options to be test and hello



What am I doing wrong?



<SCRIPT>
function runList(){
document.getElementById('list').value = <option>hello</option>;
}
</SCRIPT>

<FORM NAME=myform ACTION= METHOD=GET>
Your Options:
<INPUT TYPE=button NAME=button VALUE=Click onClick=runList()/>
<SELECT NAME=list ID=list>
<OPTION>test</OPTION>
</SELECT>

More From » html

 Answers
7

You need to actually add the option object to the dom. I have linked to a fiddle with your example working: http://jsfiddle.net/DS8TG/



Change runList to the following:



function runList(){
var select = document.getElementById('list');
select.options[select.options.length] = new Option('Hello', 'Hello');
}​

[#84976] Sunday, June 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denis

Total Points: 260
Total Questions: 87
Total Answers: 87

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;