Click here to Skip to main content
15,887,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How do I change drop down list content based on radio button click.

my code :

XML
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Tes
 */
@WebServlet("/Tes")
public class Tes extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Tes() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //HttpSession sess = request.getSession(true);
        //int id = 5;
        //sess.setAttribute("id", id);
        //response.sendRedirect("det.html");






        PrintWriter outid = response.getWriter();
        outid.println("<!DOCTYPE html>"
        +"<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");

        outid.println("<script type='text/javascript' src='dummy.js'></script>"
        +"<link rel='stylesheet' type='text/css' href='result-light.css'>"
        +"<style type='text/css'>"
        +"</style>"



+"<script type='text/javascript'>"+"//<![CDATA["

+"(function() {"
+"var radIn = null, sel = null, current = false"
+"function init() {"
+"var radOut = document.getElementById('radOut');"
+"radIn = document.getElementById('radIn');"
+"sel = document.getElementById('dropDown');"

+"radIn.addEventListener('change', onChange);"
+"radOut.addEventListener('change', onChange);"
+"onChange();"
+"}"
+"function onChange() {"
+"var rad = radIn.checked;"
+"if(rad == current)"
+"return;"
+"current = rad;"
+"var array = rad ?"
+"['in1','in2','in3','in4','in5'] : "
+"['out1','out2','out3','out4','out5'];"
+"sel.innerHTML = '';"
+"array.forEach(function(k) {"
            //alert(k + ' asdsd');
            +"var opt = document.createElement('option');"
            +"opt.innerHTML = k;"
            +"sel.appendChild(opt);"
            +"})"
            +"}"
            +"window.addEventListener('load', init);"
            +"})();"
            +"//]]>"

            +"</script>"

+"<style type='text/css'></style></head>"
+"<body>"
        +"<input type='radio' value='in' name='campus' id='radIn' checked='checked'>"
        +"<label for='radIn'>In Campus</label>"
        +"<br>"
        +"<input type='radio' value='out' name='campus' id='radOut'>"
        +"<label for='radOut'>Out Campus</label>"
        +"<br>"
        +"<select id='dropDown'><option>in1</option><option>in2</option><option>in3</option><option>in4</option><option>in5</option></select></body></html>");







    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
Posted
Comments
Siddharth.10 11-Nov-15 3:24am    
The script is not running at all here(however it does work in a separate .html file)..problem with the quotes perhaps?
...any help would be much appreciated.
Richard MacCutchan 11-Nov-15 5:33am    
What dropdown list, which radio button? Please do not just dump your code and expect people to understand what it is supposed to do. Edit your question and provide a proper detailed explanation of the probblem.
Gokulprasad05 14-Nov-15 7:05am    
Campus:
<input type="radio" name="Campus" value="in" />In Campus
<input type="radio" name="Campus" value="out" />Out Campus

Java code in servlet:

String Campus = request.getParameter("Campus");
System.out.println("Campus is: " + Campus);

1 solution

use onchange



<body>
<input type="radio">
onchange="document.getElementById('e').hidden=1;document.getElementById('d').hidden=0;
document.getElementById('b').checked=0;"
value="x" id="c">x</input>
<input type="radio">
onchange="document.getElementById('d').hidden=1;document.getElementById('e').hidden=0;
document.getElementById('c').checked=0;"
value="y" id="b">y</input>
<select id="d"><option>1</option>
<option>2</option>
</select>
<select id="e"><option>3</option>
<option>4</option>
</select>

</body>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900