PDA

View Full Version : Get the value of 1st item in dropdown menu


Coral_Lover
10-01-2002, 10:49 AM
Hi, does anyone of u know how to get the value of the 1st item in a dropdown menu? I'm using ONCHANGE event. Is it possible?? Coz i need to get the value of the 1st item to be stored in some JSP variables so that some processing can be done.

glenngv
10-01-2002, 10:57 AM
document.YourFormName.YourSelectName.options[0].value

Coral_Lover
10-02-2002, 02:10 AM
Glenn, i've tried that out, but it doesn't get the value of the 1st combo box. My codes are below. Could u please take a look, and is it bcos i've typed something wrong? Please correct me. Thanks!

function retGrp(thisForm)
{
var gForm = document.AddHost;
gForm.submit();

AddHost.hideGrp.value = document.AddHost.cboGrp.option[0].value;
if (AddHost.cboGrp.selectedIndex !=0)
{
<%
hDep = request.getParameter("cboDep");
hgrp = request.getParameter("cboGrp");
hApp = request.getParameter("txtApp");
%>
}
else
{
<%
hDep = request.getParameter("hideGrp");
hgrp = request.getParameter("cboGrp");
hApp = request.getParameter("txtApp");
%>
}

}

whammy
10-02-2002, 02:12 AM
The problem there would be that you are submitting the form before you check the other options...:


var gForm = document.AddHost;
gForm.submit();


Once you submit the form, anything after that is not computed, since the form was submitted to whatever page you specified... timing is important!

It's kind of like that commercial where the girl says "I love you!" and the guy waits until she gets mad and leaves the restaurant, and then looks at her empty chair and says "I love you too!".

Coral_Lover
10-02-2002, 02:17 AM
Hmm.. but it works fine if i select the other items from the combo box. The main problem is that i couldn't invoke the event handler, ONCHANGE whenever i select the 1st item of the combo box. But if i select the others, it will capture the value and do the required JSP processing.

whammy
10-02-2002, 02:24 AM
That should solve your problem! :D

Think about what you just said, and alert or write values to your page to see what values you're getting. :D

Coral_Lover
10-02-2002, 02:30 AM
Hi whammy, yes, i've tried out those alerts.. But i dont know why it juz can't get the values... The alertbox were not executed :mad: BTW, what should solve my problem?? Using what method?? Sorry, i dont quite get you. :o

glenngv
10-02-2002, 02:53 AM
AddHost.hideGrp.value = document.AddHost.cboGrp.options[0].value;

whammy
10-02-2002, 10:31 AM
The reason onchange isn't working for you is because the first value of a select dropdown is selected by default (unless you have specified otherwise)... so onchange won't fire if you still have that selected (since the value hasn't changed!).

The easiest solution for this is to make the first field not have a value... example:

<select name="whatever" onchange="functionName(this.options[this.selectedIndex]">
<option value="">Select One</option>
<option value="One">One</option>
<option value="Two">Two</option>
</select>