Solved

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

Posted on 2015-02-14
3
2,329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 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 27

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

726 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