Issue
I have select in my index page. It looks like below
<div id="dropdown" class="input-group" style="width:50%">
<select class="form-control" id="select2" name="select2" onchange="javascript:document.form1.submit();" style="width : 250px">
<option value="C:/path1" >option 1/option>
<option value="C:/path2" >option 2</option>
<option value="C:/path3" >option 3</option>
<option value="C:/path4" >option 4</option>
<option value="C:/path5" >option 5</option>
<option value="C:/path6" >option 6</option>
</select>
</div>
I need value of "Value" attribute i.e., "C:/path1" which I'm able to fetch in Servlet using
String value= request.getParameter("select2");
Now I need to fetch text between tag i.e., I need "option 1" or anything based on selection. How can I do it. Kindly help. Thanks in advance :)
Solution
With form you can only value
of selected option if you want the option label as well then you need to write extra logic to send the data to server.
<div id="dropdown" class="input-group" style="width:50%">
<input type="hidden" name="selectedLabel" id="selectedLabel">
<select class="form-control" id="select2" name="select2" onchange="javascript:getSelectedLabel(this);" style="width : 250px">
<option value="C:/path1" >option 1/option>
<option value="C:/path2" >option 2</option>
<option value="C:/path3" >option 3</option>
<option value="C:/path4" >option 4</option>
<option value="C:/path5" >option 5</option>
<option value="C:/path6" >option 6</option>
</select>
</div>
JS: using this function you set the option label to hidden field and the hidden field will be sent along with your form data.
function getSelectedLabel(sel) {
document.getElementById("selectedLabel").value = sel.options[sel.selectedIndex].text;
document.form1.submit();
}
Server side:
String value= request.getParameter("select2");
String label = request.getParameter("selectedLabel");
Answered By - Srinu Chinka