[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to retain checked checkboxes after submitting the form to same page in jsp ?

Posted on 2015-02-14
3
Medium Priority
?
2,821 Views
Last Modified: 2015-02-15
I have a form in JSP.
When I check any 3 checkboxes and submit the form I want the checked checkboxes to retain their state.
How to achieve this using javascript ?


<html>
<body>

<form action="">
<input type="checkbox" name="axes" value="Time">Time<br>
<input type="checkbox" name="axes" value="Product">Product<br> 
<input type="checkbox" name="axes" value="Location">Location<br> 
<input type="checkbox" name="axes" value="Sore">Store<br> 
<input type="submit" value="submit">
</form>

</body>
</html>

Open in new window

0
Comment
Question by:Pradip Shenolkar
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 40610982
Please try this code.
<%@ page import="java.util.*" %>
<html>
<body>
<% 
   String[] axes = request.getParameterValues("axes");
   if (axes != null) 
   {
   	  String check = "checked='checked'";
      List list = Arrays.asList(axes);
	  if(list.contains("Time"))pageContext.setAttribute("time", check);
	  if(list.contains("Product"))pageContext.setAttribute("product", check);	
	  if(list.contains("Location"))pageContext.setAttribute("location", check);
	  if(list.contains("Store"))pageContext.setAttribute("store", check);		 
   }
%>
<form>
<input type="checkbox" name="axes" value="Time" ${time}/>Time<br>
<input type="checkbox" name="axes" value="Product" ${product}/>Product<br> 
<input type="checkbox" name="axes" value="Location" ${location}/>Location<br> 
<input type="checkbox" name="axes" value="Store" ${store}/>Store<br> 
<input type="submit" value="submit">
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Pradip Shenolkar
ID: 40611075
It solved the problem but it would be more helpful to me from my project perspective if the solution is achieved using javascript.
0
 
LVL 28

Expert Comment

by:rrz
ID: 40611090
When the client submits the form, he creates a new request. The Javascript values from the previous page are lost. To use Javascript, you would have to store the data somewhere. You could use cookies or use localStorage (HTML5). Look at
http://www.tutorialspoint.com/javascript/javascript_cookies.htm
http://www.thesitewizard.com/javascripts/cookies.shtml
http://www.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/     
https://robert.accettura.com/blog/2012/01/17/localstorage-with-cookie-fallback/
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question