Page 1 of 1

<select ...<option (tag problem)

Posted: Sun Jun 15, 2008 6:22 am
by toraj58
i have problem with this javascript code. i can not access to this value : window.document.theform.select1.op2.value; and i got error! please help me.

Code: Select all

 
<script language = "javascript">
 
function disp()
{
//document.write('information was sent');
window.document.theform.txt1.value = "admin logged!";
window.document.theform.txt2.value = window.document.theform.select1.value;
window.document.theform.txt3.value = window.document.theform.select1.op2.value; [color=#FF0000]// this line of code causes error[/color]
}
 
</script>
 
<form action='test.php' method = 'post' name='form1' id='theform'>
<input type = 'text' id='txt1' name='textbox1'>
<p>
<input type = 'text' id='txt2' name='textbox2'>
<p>
<input type = 'text' id='txt3' name='textbox3'>
<p>
 
<select name="Greeting" id="select1">
    <option id = "op1" name = "nop1" value="Hello">Hello</option>
    <option id = "op2" name = "nop2" value="Hola">Hola</option>
    <option id = "op3" name = "nop3" value="Bonjour">Bonjour</option>
</select>
 
<input type = 'button' id='btn' name = 'button1' value = 'sendit' onclick = 'disp();'>
</form>
 

Re: <select ...<option (tag problem)

Posted: Sun Jun 15, 2008 6:33 am
by superdezign
Try using document.getElementById("op2").

Re: <select ...<option (tag problem)

Posted: Sun Jun 15, 2008 6:49 am
by toraj58
tnx, it works...another question how i can extract name(not value) of selected option and put it into textbox2?

Re: <select ...<option (tag problem)

Posted: Sun Jun 15, 2008 9:36 am
by kaszu
Use innerHTML

Code: Select all

document.getElementById("txt2").value = document.getElementById("op2").innerHTML