newbie 22
newbie 22

Reputation: 21

How to get text between <option> tag in servlet </option>

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 :)

Upvotes: 1

Views: 1767

Answers (2)

Srinu Chinka
Srinu Chinka

Reputation: 1491

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"); 

Upvotes: 1

SandyKrish
SandyKrish

Reputation: 232

here the simple and quick solution.

Try this

<select class="form-control" id="select2" name="select2" onchange="javascript:document.form1.submit();" style="width : 250px">
                    <option value="C:/path1_option 1" >option 1/option>
                    <option value="C:/path2_option 2" >option 2</option>
</select>

If your send the data to servlet like this (path + option text) the you can get the data by request.getParameter() method and split the data using java code.

String value= request.getParameter("select2");
String vals[]  = value.split("_",0);

for(String v : vals){
    System.out.println(v);
}

so, you can get these two values....

Upvotes: 0

Related Questions