Hi! can anyone tell me what is wrong with the code? It has something to do with the way i call the value of the selected option but i cannot track the problem.. <html> <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 "> <head> <script type="javascript"> function foo(l,c) { var z=document.getElementById(c) alert(l.z.options[l.z.options.selectedIndex].value) } </script> </head> <body> <form id="ghost"> <select name="bery" id="sel" onchange="foo('ghost','sel')"> <option>1</option> <option>2</option> </select> </body> </form> </html> Code (markup):
1) use <script type="text/javascript"> instead of <script type="javascript"> 2) add semicolon at the end of each operator 3) place </form> tag before </body> 4) use innerHTML to access content of option tags <html> <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 "> <head> <script type="text/javascript"> function foo(objName) { var sel=document.getElementById(objName); var index = sel.selectedIndex; alert(sel.options[index].innerHTML); } </script> </head> <body> <form id="ghost"> <select name="bery" id="sel" onchange="foo('sel')"> <option>1</option> <option>2</option> </select> </form> </body> </html>
<html> <meta http-equiv="Content-Type" content="text/html; charset=windows-1255 "> <head> <script> function foo(l,c){ alert(document.forms[l].elements[c].value) } </script> </head> <body> <form name="ghost"> <select name="sel" onchange="foo('ghost','sel')"> <option value="1">1</option> <option value="2">2</option> </select> </form> </body> </html> u can put this foo(this) and in function foo function foo(object) { alert(this.value) }