Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Box Problem! 1

Status
Not open for further replies.

chmanio

Programmer
Dec 16, 2002
20
0
0
PH
My form code:

<form name="frm_term">
<select name="c_term" class="flyoutSubHeading"
<option>1st Semester, SY 2003-2004</option>
<option>2nd Semester, SY 2003-2004</option>
<option>1st Semester, SY 2004-2005</option>
<option>2nd Semester, SY 2004-2005</option>
</select>
</form>

my JavaScript code:

<SCRIPT language=JavaScript>
function sprex() {
var comboValue;
var selIndex = document.frm_term.c_term.selectedIndex;
comboValue = frm_term.c_term.options[selIndex].value;
return(comboValue);
}
</SCRIPT>


my problem: when i try to execute alert(sprex())
the javascript dont return any value..
can anybody tell me why?

thanks!

chris
 
1. Do you mean it alerts "" or that there is no alert and an error?
2. What browser are you using?
3. Try
Code:
function sprex() 
{
  var sel = document.forms['frm_term'].elements['c_term'];
  return sel.options[sel.selectedIndex].value;
}

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
thanks for the quick reply.

it alerts "".

chris
 
Does the function work? Does the selected item have a value?

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
I don't think you're reference the value correctly. It would take the form:

Code:
myDocument.myForm.mySelect.options[mySelect.selectedInded].value

You need to "return" that value, and use the "return" keyword in your function call, as well:

Code:
alert(return myFunction());


Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
You definitely wouldn't need the return in the alert. The problem may be incorrect references or an incorrect return statement (return(comboVal) instead of return comboVal).

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
hey guys, ive re-written my code to:

<SCRIPT language=JavaScript>
function sprex() {
var comboValue;
var sel = document.forms['frm_term'].elements['c_term'];
comboValue = sel.options[sel.selectedIndex].value;
return(comboValue);
}
</SCRIPT>

still returns "".

the function works i guess coz when i tried to
alert(sel.selectedIndex) it returns a value.
 
Do the options have values?

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
yeah, the values are:

1st Semester, SY 2003-2004
2nd Semester, SY 2003-2004
1st Semester, SY 2004-2005
2nd Semester, SY 2004-2005

im populating the combo using JSP <pulling data from oracle>
 
So your page looks like this?
Code:
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function sprex()
{
  var sel = document.forms['frm_term'].elements['c_term'];
  return sel.options[sel.selectedIndex].value;
}
// -->
</script>
</head>
<body>
<form name="frm_term" id="frm_term">
 <select name="c_term">
  <option value="1st Semester, SY 2003-2004>1</option>
  <option value="2nd Semester, SY 2003-2004>2</option>
  <option value="1st Semester, SY 2004-2005>3</option>
  <option value="2nd Semester, SY 2004-2005>4</option>
 </select>
</form>
<a href="#" onclick="alert(sprex()); return false;">Alert the value</a>
</body>
</html>

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
this:

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="default.css" rel="stylesheet" type="text/css">
<p>

<form name="frm_term">
<select name="c_term" class="flyoutSubHeading" onChange=alert(sprex())>

<option>1st Semester, SY 2003-2004</option>
<option>2nd Semester, SY 2003-2004</option>
<option>1st Semester, SY 2004-2005</option>
<option>2nd Semester, SY 2004-2005</option>
</select>
</form>
</head>


<SCRIPT language=JavaScript>
function sprex() {
var comboValue;
var sel = document.forms['frm_term'].elements['c_term'];
comboValue = sel.options[sel.selectedIndex].value;
alert(sel.selectedIndex);
return comboValue;
}
</SCRIPT>

</p>

<p>&nbsp; </p>
</body>
</html>
 
Your options have no values! They have "text".
Call
Code:
sel.options[sel.selectedIndex].[red]text[/red]
or add a value="x" attribute to each option.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
hey! your last suggestion solved the problem!

[2thumbsup]

whats the difference between value and text property?

Thanks!
 
Code:
Sample option:
<option [red]value="a"[/red]>[blue]b[/blue]</option>

alert(option.value); // alerts "a"
alert(option.text); // alerts "b"
When submitting the form (either get or post method), the value passed to the server is the stuff in the value attribute. The text, or anything between the two option tags, is what the user sees.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
i see.. whew!

thanks for the reply guys.[bigcheeks]

[peace]

chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top