Reputation: 41
I am using three dropdown lists which are interdependent.
User has to select first dropdown list. Accordingly the second list will be populating.
Then again user will select second dropdown list then the third list will be populate.
At the third dropdown list selection the result will populate. For the entire dropdown list I am using Ajax. But for the last dropdown list I am using action
so I have to submit the form.
Once form is returning back after the search result. The results are population properly.
But the last two dropdown lists are not populating with selected value.
So for that I am assigning the value at the time of unload of page as below
document.getElementById("laneid").text = '<%=laneID%>';
I have tried this also
document.getElementById("laneid").value = '<%=laneID%>';
befor coming back from the action i am calling ajax to populate all the values in drop down. values are coming in the request variable '<%=laneID%>'
.
Please help me to retain the values.
I think jQuery can help in this case. but if there is any other solution for this then please suggest.
Thanks a ton.
Upvotes: 4
Views: 5306
Reputation: 7187
Romy,
The SELECT can't be accessed using
document.getElementById("laneid").text = '<%=laneID%>';
document.getElementById("laneid").value = '<%=laneID%>';
Instead you should use.
var selLaneID = document.getElementById("laneid"); // get the element
selLaneID.options[selLaneID.selectedIndex].value; // gives the value of selected option
selLaneID.options[selLaneID.selectedIndex].text; // gives the selected text
In JQuery:
$("#laneid").val(); // gives value of selected text
$("#laneid option:selected").text(); // gives the selected text
$("#laneid").text(); // gives ALL text in SELECT
$("#laneid").val("val3"); // selects the option with value="val3"
if you want to set the dropdon value in JavaScript, using JQuery method will be easiest.
Using Struts, you can also populate and select dropdown using <html:select>
and <html:optionsCollection>
tags. Do check it out.
Upvotes: 1