Solved

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

Posted on 2015-02-14
3
2,230 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

739 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