Click to See Complete Forum and Search --> : capture selected option values from list menu


mistgal
01-01-2004, 11:31 PM
hi..

i would like to capture the selected option values from the list menu..

this is the codings..
<%
out.println("<select name='site_name' >");
out.println("<option>=====Actual Site Name=====</option>");
for(int i=0;i<actualAL.size();i++)
{
out.println("<option>"+actualAL.get(i)+"</option>");
}
out.println("</select>");
%>

this is where i would like to capture the values.. (its all on the same page)
<input type="hidden" name="message" value="User: <%=session.getAttribute("adUsername")%> Contract Insert: ">

thks..

Khalid Ali
01-02-2004, 08:11 AM
capturing a value is no problem,however ,to me, your question seems a bit vague.

Here is a simple description for the task you mentioned.

If you add the following in the select box code whenever you select something it will give u an alert box with selected value(I am guessing u are using JSP)

out.println("<select name='site_name' onchange='alert(this.options[this.selectedIndex].text)'>");

Lets do a little analysis of what I did
keep the following statement in mind

onchange='alert(this.options[this.selectedIndex].text)'

1.onchange = I added an event initialiser for this html element,which you can tell will be triggered everytime there is a chagne in the state of the element e.g selecting a value.

2. this -> points to itself or the element where we are running the script,which gives us the control over all the properties related to this element.

3. options is the array of all the options in the list box

4. this.selectedIndx gives us the index number of the option that is selected

5. hence we have no control over the value and ext properties of the option a user may select.

6 . .text is the text property,if you have value property set you can swap text with value as well.

Hope this helps

mistgal
01-03-2004, 12:28 AM
hi..

i try uising the
onchange='alert(this.options[this.selectedIndex].text)' mtd.. but its not wat i wanted..

erm.. wat i was trying to say is whenever a user select a value from the list menu.. the selected value will be capture on the 'hidden' fields which is also in the same page..

let mi noe if u stil dun get wat i meant..

thank you so much...

Khalid Ali
01-03-2004, 09:49 AM
Idid get wha tyou meant earlier,however, I thought it would be better if you at least tried it your self...anyays suppose the hidden fields name is
hvalue;
now we can set the selected value from the list box to this field just as we showed an alert box.
instead of showing the alert box we will just assign this value to the field this is the onchange event before

onchange='alert(this.options[this.selectedIndex].text)'

change it to like this

onchange='this.form.hvalue.value=(this.options[this.selectedIndex].text)'