|
-
Feb 27th, 2008, 10:10 PM
#1
Thread Starter
Fanatic Member
Javascript, how to get value of combobox
Dear All,
I am learning javascript and now trying to get the value of selected combo box using below javascript.. but it does not work.. could any body please help?
function JSGetSelectedItem() {
alert("Hello JSCript " + document.form1.select.options(document.form1.select.selectedindex).text);
}
===
and I call it from my PHP codes..
<select name="select" size="1" onchange="JSGetSelectedItem()">
<?php
for ($i=1;$i<=10;$i++)
{
echo "<option>".$i;
}
?>
</select>
-
Feb 27th, 2008, 10:20 PM
#2
Re: Javascript, how to get value of combobox
You've no closing </option> tags.
-
Feb 27th, 2008, 10:46 PM
#3
Thread Starter
Fanatic Member
Re: Javascript, how to get value of combobox
Yes.. I knew .. I just want to make it simple ..
I tried to change it to
echo "<option>".$i."</option>";
but still unable to get the value of the combo box.. it always return "Hello JScript 1"
any idea?
Thanks & Regards
Winanjaya
-
Feb 28th, 2008, 04:49 AM
#4
Re: Javascript, how to get value of combobox
Hi there Winanjaya,
have a look at this example it may, hopefully , answer your problem...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function() {
var df=document.forms[0][0]; /* this is another method for accessing form elements*/
df.onchange=function() {
val=df.options[df.selectedIndex].value;
txt=df.options[df.selectedIndex].text;
alert('without a set "option value", \nOpera and Firefox take...\n\n "value" to refer to the "option text"'+
' ...\n\n '+val+'\n\n...IE returns nothing,\n\n but will submit the "text" as a "value"');
alert('IE will only respond to "text" to give "option text"...\n\n '+txt);
}
}
</script>
</head>
<body>
<form action="http://www.google.com/" method="get"> <!--google is good for testing form submissions-->
<div>
<select name="select" >
<option>option one</option>
<option>option two</option>
<option>option three</option>
<option>option four</option>
<option>option five</option>
</select>
<input type="submit">
</div>
</form>
</body>
</html>
coothead
-
Feb 28th, 2008, 07:38 AM
#5
Thread Starter
Fanatic Member
Re: Javascript, how to get value of combobox
Hello,
just want to share..
this codes below solved my problem..
any way .. thanks a lot!
Regards
Winanjaya
function JSGetSelectedItem() {
var dropdownIndex = document.getElementById('select').selectedIndex;
var dropdownValue = document.getElementById('select')[dropdownIndex].text;
alert("Hello JSCript " + dropdownValue);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|