By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

How to retrieve "option value" info from select tag

P: 8
Hi guys!
How I can retrieve "option value" info from select tag?
I am using this code, but it is only let me get "name".
I need to pass this value to second select tag.
I found this code here:
http://adamv.com/dev/javascript/http_request
but they do not have any instructions how to retrieve option values.

----------JS------------

[HTML]<script>
function handleOnChange(dd1)
{
var idx = dd1.selectedIndex;
var val = dd1[idx].text;
var par = document.forms["frmSelect"];
var parelmts = par.elements;
var prezsel = parelmts["prez"];
var course = val;
if (course != "-Select a Course-")
{
Http.get({
url: "./" + course + ".txt",
callback: fillPrez,
cache: Http.Cache.Get
}, [prezsel]);
}
}

function fillPrez(xmlreply, prezelmt)
{
if (xmlreply.status == Http.Status.OK)
{
var prezresponse = xmlreply.responseText;
var prezar = prezresponse.split("|");
prezelmt.length = 1;
prezelmt.length = prezar.length;
for (o=1; o < prezar.length; o++)
{
prezelmt[o].text = prezar[o];
}
}
else
{
alert("Cannot handle the AJAX call.");
}
}
</script>[/HTML]

------HTML----------

[HTML]<form name="courseSelect">

<select name="course" onChange.value="handleOnChange(this);">

<option value='0'>-Select a Course-</option>
<option value='1'>Course Name 1</option>
<option value='2'>Course Name 2</option>
<option value='3'>Course Name 3</option>

</select>

</form>
[/HTML]

Thank you

Sergey.
Aug 5 '08 #1
Share this Question
Share on Google+
6 Replies


samikhan83
P: 33
hi..
1. you have to first change onchange.value TO only onchange.
2. U can get the value of th select field easily by using document.getElementById.value....
Aug 6 '08 #2

P: 8
Thank you samikhan83.
I am newbyte in JS & AJAX
Can you please post some example?
Aug 6 '08 #3

RamananKalirajan
100+
P: 607
Hi Dude, hope this code may help you. If u have any doubts post it I will surely help u out.

[HTML]<html>
<head>
<script language="javascript">
function doThis()
{
var sel = document.getElementById('mySelect1').selectedIndex ;
var selObj = document.getElementById('mySelect1').options[sel].text;
document.getElementById('mySelect2').add(new Option(selObj,selObj));
document.getElementById('mySelect1').remove(sel);

}
</script>
</head>
<table>
<tr>
<td>
<select id="mySelect1" size="10" onclick="doThis()">
<option>Apple</option>
<option>Orange</option>
<option>Mango</option>
<option>Grape</option>
<option>Pine</option>
</select>
</td>
<td>
<select id="mySelect2"size="10" >
</select>
</td>
</tr>
</table>
</html>[/HTML]

Regards
Ramanan Kalirajan
Aug 6 '08 #4

acoder
Expert Mod 15k+
P: 16,027
Change line 5 to:
Expand|Select|Wrap|Line Numbers
  1. var val = dd1[idx].value;
or replace lines 4 and 5 with:
Expand|Select|Wrap|Line Numbers
  1. var val = dd1.value;
Aug 6 '08 #5

P: 8
Change line 5 to:
Expand|Select|Wrap|Line Numbers
  1. var val = dd1[idx].value;
or replace lines 4 and 5 with:
Expand|Select|Wrap|Line Numbers
  1. var val = dd1.value;

!!! THANK YOU VERY MUCH ACODER !!!
Aug 6 '08 #6

acoder
Expert Mod 15k+
P: 16,027
You're welcome :) Post again if you have more questions.
Aug 6 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.