0

I have read somewhere in stackoverflow that we will not get the value of checkbox which is display:none using javascript but I am getting the value of checkbox in servlet.

Below is my JSP code

<script type="text/javascript">
function hideData(){
 document.getElementById("checkBoxTest").style.display="none";

}
</script>

<form action="DisplayServlet" method="get" >

<input type="checkbox" value="CheckboxTest" id ="checkBoxTest" name = "checkBoxTest" checked/>Test <br/>
<a href="#" onclick = "return hideData();">Click to hide check box </a> <br/>
<input type="submit" value="Submit" />
</form>

But I am getting values of checkbox even if I hide it. But somewhere in stackoverflow I read that visibility:hidden will give value whereas display:none will null values.

My servlet code where I am getting values.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String checkBoxValue = request.getParameter("checkBoxTest");
    System.out.println("checkBoxValue ====== "+checkBoxValue);
}
pise
  • 849
  • 6
  • 24
  • 51

1 Answers1

0

I guess you have misunderstood the display:none

display:none means that the the tag in question will not appear
on the page at all (although you can still interact with it through the dom). 
There will be no space allocated for it between the other tags.

Also to your question ,

Will I get the display: none input value in servlet?

No, you won't .

Try this as you might miss clicking the href button , add the javascript function to your form as

<form action="DisplayServlet" method="get" onsubmit="return hideData();">

else to your input tag as ,

<input type="checkbox"  value="CheckboxTest" id ="checkBoxTest" name = "checkBoxTest" style="display: none"/>

Hope this helps !!

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • I am getting values of checkbox I have added servlet code. Please see – pise Apr 10 '14 at 10:54
  • I have a form where user can check the field he want and the one which he unchecked should not be process in servlet. But I am getting the values of one which is unchecked by user also. Now how to identify which one is unchecked by user since I am getting values for all the input field. – pise Apr 10 '14 at 11:01
  • @pise you want to pass the value of the checkbox if it is selected ? am i correct ? – Santhosh Apr 10 '14 at 11:07
  • no need to specify anything . By default if you haven't checked your check box it wont print anything in the servlet – Santhosh Apr 10 '14 at 11:12
  • if user has click on href then I dont want check box value but he has not clicked hidden I want the value of check box in servlet – pise Apr 10 '14 at 12:19
  • @pise If user clicks on the checkbox then you will get the value of it in servlet else you wont get . what else you think need to be done ? – Santhosh Apr 10 '14 at 12:21