Solved

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

Posted on 2015-02-14
3
1,779 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 27

Accepted Solution

by:
rrz earned 500 total points
Comment Utility
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
Comment Utility
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 27

Expert Comment

by:rrz
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now