<select ...<option (tag problem)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
toraj58
Forum Newbie
Posts: 12
Joined: Sun Jun 15, 2008 12:40 am

<select ...<option (tag problem)

Post 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>
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

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

Post by superdezign »

Try using document.getElementById("op2").
toraj58
Forum Newbie
Posts: 12
Joined: Sun Jun 15, 2008 12:40 am

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

Post by toraj58 »

tnx, it works...another question how i can extract name(not value) of selected option and put it into textbox2?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

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

Post by kaszu »

Use innerHTML

Code: Select all

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