By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

Recover checkboxes on pageload

P: 57
I understand there are several ways to save checkbox information. The way that best fits this application is validating on the divID but the function is still not working.

Expand|Select|Wrap|Line Numbers
  1. function checkCB() {
  2.           var inputs = document.getElementById(Filtered).getElementsByTagName('input');
  3.            for (var i=0; i<inputs.length ; i++)
  4.                {
  5.                var obj = inputs[i];
  6.                if(inputs[i].type == "checkbox") {
  7.                inputs[i].checked=false;
  8.                }
  9.            }
  10.       }
  11.  
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <input 
  4.     type="checkbox"
  5.     class="checkbox"
  6.     id="Filtered"
  7.     name="title"
  8.     value="false"
  9.     /> 
  10.       title 
  11.  
Jan 23 '08 #1
Share this Question
Share on Google+
11 Replies


acoder
Expert Mod 15k+
P: 16,027
"Filtered" should be in quotes.
Jan 24 '08 #2

P: 57
I managed to get the field in the URL showing the correct value onsubmit and the view source is correct as well. But the checkbox itself remains empty.


Expand|Select|Wrap|Line Numbers
  1. function checkCB() {
  2.       var inputs = document.getElementById("Filtered").getElementsByTagName('input');
  3.         for (var i=0; i<inputs.length ; i++)
  4.                {
  5.          var obj = inputs[i];
  6.         if(inputs[i].type == "checkbox") {
  7.          inputs[i].checked="false"; 
  8.                }
  9.            }
  10.        }
  11.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. <body onload="checkCB()"> 
  3.  
  4. <%     
  5. String title = request.getParameter("title");
  6.         if (title != "false") 
  7.         {
  8.     title = "true";
  9.         }
  10. %>
  11.  
  12. <input                    
  13.         type="checkbox"
  14.     class="checkbox"
  15.     id="Filtered"
  16.     name="title"
  17.     value="<%=title %>"
  18.        />     
  19.     title 
  20.  
Jan 24 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Filtered is the name of the checkbox, so if you try to get the input elements from there, you won't get anything. Look for the parent element container for the checkbox and then use getElementsByTagName.
Jan 25 '08 #4

P: 57
I do appreciate your feedback aCoder although I'm not sure I understand what I need to key in on.
Jan 25 '08 #5

acoder
Expert Mod 15k+
P: 16,027
What exactly is checkCB() supposed to do? In your code, setting checked to false would mean that the checkbox is unchecked.
Jan 25 '08 #6

P: 57
Once the checkbox is checked, the form is submitted to a second form. The second form includes the checkbox and I need it to be checked or unchecked depending on the user selection.

I must be getting mixed up if I am setting the box to false in my function.
Jan 25 '08 #7

acoder
Expert Mod 15k+
P: 16,027
In that case, you don't even need JavaScript (though you could use JavaScript too).

A checkbox is only passed through if it is checked, so in your JSP code, check that the value exists. If it does, set the checked attribute to "checked" or "true" for the checkbox using JSP.
Jan 25 '08 #8

P: 57
I have this working but is there a cleaner way to recheck checkboxes on pagination?

Expand|Select|Wrap|Line Numbers
  1. <%
  2. String[] categories = request.getParameterValues("category");
  3.         String category = "";
  4.     if(categories != null)
  5.     {
  6.         for(int ii = 0; ii < categories.length; ii++)
  7.         {
  8.         category += categories[ii];
  9.         }
  10.                 }
  11. %>
  12. <div class="item">
  13.      <input 
  14.     type="checkbox"  
  15.     id="cb1" 
  16.     name="category"
  17.     value="cb1"
  18.     <%  if (categories.indexOf("cb1") == -1) {%>
  19.     <%  } else { %>
  20.     checked="yes"
  21.     <% } %>
  22.         Checkbox 1
  23. </div>
  24. <div class="item">
  25.      <input 
  26.     type="checkbox"  
  27.     id="cb2" 
  28.     name="category"
  29.     value="cb2"
  30.     <%  if (categories.indexOf("cb2") == -1) {%>
  31.     <%  } else { %>
  32.     checked="yes"
  33.     <% } %>
  34.        Checkbox 2
  35. </div>
  36.  
Mar 12 '08 #9

acoder
Expert Mod 15k+
P: 16,027
The cleaner way would be to only check if checked:
Expand|Select|Wrap|Line Numbers
  1.      <input 
  2.     type="checkbox"  
  3.     id="cb1" 
  4.     name="category"
  5.     value="cb1"
  6.     <%  if (categories.indexOf("cb1") != -1) {%>
  7.     checked
  8.     <% } %>>
  9.         Checkbox 1
  10.  
Don't forget to close the tag.
Mar 12 '08 #10

P: 57
a few more lines of code you've cleaned up for me... and counting.

Thanks for the reply.
Mar 14 '08 #11

acoder
Expert Mod 15k+
P: 16,027
You're welcome!
Mar 16 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.