How to set value on select change of drop down in JSP

80,813

Solution 1

1st thing first, If you want to set value to the dropdown on change of value, well it does that itself. But if you want to set the value selected to session, again it does that itself all you have to do is use request.getParameter(color) on the next page or backend where you processing request. For printing on console, you have correct code. Only close your tags properly.

Solution 2

Try This and let me know..

<script type="text/javascript">
function setValue(){
document.getElementById("dropdown").value=document.getElementById("colour").value;
document.productForm.submit();
return true;
}
</script>

<form method="post" action="index.jsp" name="productForm">
    <select id="colour" name="colour" onchange="return setValue();">
        <option value="dropdown">Pls select one
        <option value="apple">Apple
        <option value="oragne">Orange
        <option value="grapes">Grapes
    </select>
    <input type="hidden" name="dropdown" id="dropdown">
    <input type="submit" value="click" name="btn_dropdown">
    <form>

  <%
        String colour = request.getParameter("colour").toString();
        out.println(colour);
   %>

Solution 3

form and all option tags are not closed

Example for correct markup of option:

<option value="apple">Apple</option>

Your updated code should be something like this:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <form method="post" action="index.jsp" name="productForm">
            <select name="colour" id="dropdown">
                <option value="dropdown">Pls select one</option>
                <option value="apple">Apple</option>
                <option value="oragne">Orange</option>
                <option value="grapes">Grapes</option>
            </select>
            <input type="submit" value="click">
        </form>
        <%
        String colour = request.getParameter("colour");
        out.println(colour);
        %>
        <script>
        document.getElementById("dropdown").value = '<% out.print(colour); %>';
        </script>
    </body>
</html>
Share:
80,813
Aman Kumar
Author by

Aman Kumar

Updated on July 11, 2022

Comments

  • Aman Kumar
    Aman Kumar almost 2 years

    This is my code for index.jsp. I want it so that when I select an option in the drop-down menu, the value should be printed out and also the value should be set. For example, if we select "grapes" then it should print Grapes and set the value to Grapes. I have tried many things but have been unable to do so.

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <form method="post" action="index.jsp" name="productForm">
        <select name="colour" onchange="document.productForm.submit();">
            <option value="dropdown">Pls select one
            <option value="apple">Apple
            <option value="oragne">Orange
            <option value="grapes">Grapes
        </select>
        <input type="hidden" name="dropdown" id="dropdown">
        <input type="submit" value="click" name="dropdown" id="dropdown">
        <form>
        <%
            String colour = request.getParameter("colour");
            out.println(colour);
        %>
    </body>
    </html>
    
  • Aman Kumar
    Aman Kumar almost 11 years
    i creating Jsp so please Implement my code i want set value after onselect change
  • Manoj Yadav
    Manoj Yadav almost 11 years
    I have added updated code in answer, See Your updated code should be something like this:
  • Aman Kumar
    Aman Kumar almost 11 years
    org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 31 Error is coming
  • Aman Kumar
    Aman Kumar almost 11 years
    But i want that when i select change Drop down value should set what Ever i select
  • Aman Kumar
    Aman Kumar almost 11 years
    please see my code and please Update so that once i ll select change it should Set value after Submit
  • Sandiip Patil
    Sandiip Patil almost 11 years
    I suppose you will go to next page after submit, or go to some servlet to process your request. In that case you don't need to set it here you need to catch it there using the name of your dropdown.
  • Sandiip Patil
    Sandiip Patil almost 11 years
    edited your code to close the tags properly. Now on the second page after submit, to get the value of your dropdown do : request.getAttribute('colour'); This should work
  • Manoj Yadav
    Manoj Yadav almost 11 years
    Answer updated, added <script>document.getElementById("dropdown").value = '<% out.print(colour) %>';</script> and id in select <select name="colour" id="dropdown">
  • Aman Kumar
    Aman Kumar almost 11 years
    But i Need to to take action in same Page
  • Sandiip Patil
    Sandiip Patil almost 11 years
    Even in that case. what are you using to take action? Ajax? you will get the value using getParameter() of request object. and if your page is getting refreshed and you not able persist the value to the dropdown, you may use any of the javascript, jquery functions which are suggested by ppl in other answers.
  • Aman Kumar
    Aman Kumar almost 11 years
    Its not working again value set to please select to One after submit
  • Aman Kumar
    Aman Kumar almost 11 years
    Manoj please check i am having Problem i have tried this from last 3 days But still having Problem
  • Aman Kumar
    Aman Kumar almost 11 years
    i have Tried much but am Unabl to do this please help i have tried last 1 week this Problem still i dint get solution for this
  • Sandiip Patil
    Sandiip Patil almost 11 years
    Boss how will can you have the dropdown selected if your action is same as your jsp. The same jsp gets loaded and thats why you getting the default value for dropdown.
  • Sandiip Patil
    Sandiip Patil almost 11 years
    You need to change your action, its pointing to same jsp. What action you wanna do on submit??
  • Aman Kumar
    Aman Kumar almost 11 years
    This Problem is coming only with drop down But in other i have like text field i have Print same value on action fire so i am finding such type of Solution
  • Aman Kumar
    Aman Kumar almost 11 years
    Manoj can i have ur email id so that i can send u My source code
  • Sandiip Patil
    Sandiip Patil almost 11 years
    What exactly you are doing when you say you are handling the request on same jsp? what is your backend processing after submitting the request.
  • Aman Kumar
    Aman Kumar almost 11 years
    182.18.132.100:8088/cbdtforprsite/globalsearch.aspx? please check this i have to COnvert this Module in Jsp Every thing I did Except set that Drop down action is firning On select change But not Value set
  • Aman Kumar
    Aman Kumar almost 11 years
    182.18.132.100:8088/cbdtforprsite/globalsearch.aspx? please check this i have to COnvert this Module in Jsp Every thing I did Except set that Drop down action is firning On select change But not Value set
  • Sandiip Patil
    Sandiip Patil almost 11 years
    Text box keeps the cache value, dropdown works differently. And i am sure you not submitting the page on change for text box. here you are submitting thats why its not working.
  • Sandiip Patil
    Sandiip Patil almost 11 years
    The url you have given are two sections, lower section is another jsp included in parent jsp. its using ajax to do the action and calling a servlet for that, the response is set to lower jsp. the dropdown section is getting updated with the value added to textbox. It keeps the previous value in variable and sets it with same
  • Sandiip Patil
    Sandiip Patil almost 11 years
    well you need to see how ajax works, and how jsps are included into each other. And if your jsp page is simple and not much values then you can create one more jsp similar to your index.jsp, result.jsp, which will be your action on this page. On result.jsp you get values from request and set to the textboxes or dropdowns over there
  • Ravi
    Ravi almost 11 years
    First check whether it has any value or not ? with the simple jsp code.Then, include your jasper reporting code. So,check your code properly, it seems, it worked !! but you have some coding error in your reports.
  • Manoj Yadav
    Manoj Yadav almost 11 years
    ; was missing changed out.print(colour) to out.print(colour);, try updated code
  • Aman Kumar
    Aman Kumar almost 11 years