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

How to pass the checkbox value in javascript

P: 12
Hi all,
I am retrieving some rows from DataBase using for loop in jsp and displaying it in a table format.In the output table I am also adding one more text box and checkbok with each row. In text box i have to enter some value manually and when i check its corresponding checkbox the value should get passes into another jsp.
This is a code by which i am assigning the name to textboxes and checkboxes
Expand|Select|Wrap|Line Numbers
  1. String[] arr_TextValues = new String[10];
  2. String[] arr_ChkValues = new String[10]; 
  3.  
  4. arr_TextValues[i] = "Text"+i;
  5. arr_ChkValues[i] = "Chk"+i;
where i = number of rows retrieved.

Expand|Select|Wrap|Line Numbers
  1. <td><input type=text name=<%=arr_TextValues[i]%> value=""/></td>
  2. <td><input type="checkbox" name=<%=arr_ChkValues[i]%> VALUE="0" align ="center" ></td> .
Now to check and get the value of the textbox i am checking the checkbox value using javascript as :-

Expand|Select|Wrap|Line Numbers
  1. function check()
  2.     { 
  3.                 for(l=0;l< i;l++){
  4.  
  5.  if(document.form1.chk0.checked ==true)
  6.         {
  7.  
  8.                something to retrieve the value of textbox and pass it to another jsp!!!           
  9.  
  10.  
  11.                }
how can i retrieve the value of the various checkboxex and pass it to another jsp?
any help regarding how to do this will be appriciated .
May 22 '08 #1
Share this Question
Share on Google+
17 Replies


P: 12
Hi all,
I have to check the value of the 10 checkboxes using javascript, does anyone khow how to do this ?? i am using the code :

Expand|Select|Wrap|Line Numbers
  1. function check()
  2.     { 
  3.          for(i=0;i< 10 ;i++){
  4.  
  5.         if(document.form1.chki.checked ==true)
  6.         {
  7.           alert("hi");
  8.                 }
  9.    }
  10.  
  11. }
  12.  
but it is now taking the value of "i" in (document.form1.chki.checked ==true).
Thanks in advance .
May 22 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Welcome to Bytes.

To take the i value to use for the name of the checkbox, use the elements array:
Expand|Select|Wrap|Line Numbers
  1. document.form1.elements["chk"+i].checked...
PS. merged threads.
May 22 '08 #3

P: 12
Thanks for your reply !

The problem is still not solved, it is giving me some error

'document.form1.elements[...].checked' is nill or not an object

Expand|Select|Wrap|Line Numbers
  1. function check()
  2.  
  3.     { 
  4.         no_of_row = document.form1.dev.value 
  5.  
  6.           for(l=0;l<no_of_row;l++)
  7.  
  8.           {
  9.            if(document.form1.elements["Chk"+l].checked ==true)
  10.            {
  11.  
  12.              var v=document.form1.elements[l].value;
  13.                   alert(v);
  14.  
  15.              return true;
  16.  
  17.            }
  18.  
  19.            else
  20.  
  21.            {
  22.  
  23.  
  24.  
  25.           alert('Please check the assign token');
  26.                return false;
  27.            } 
  28.  
  29.  
  30.  
  31.     }
  32.  
  33.   } 
  34.  
please suggest me what to do now .
May 22 '08 #4

acoder
Expert Mod 15k+
P: 16,027
A checkbox name can be a set of checkboxes. If you want to refer to one checkbox, you may be better off using an id, e.g.
[html]<input type="checkbox" name="Chk0" id="Chk0">[/html]and then using document.getElementById("Chk0") to access it.
May 22 '08 #5

P: 12
Thanks again!
The complication in my code is that when i fetch rows i am associating a checkbok and textbox with every row ..
now to access the data of the text box whic i enter manually i have to assign unique names to both checkbok and textbox.
please have a look into the code .

in this code i have to fetch the value of the text box if its corresponding checkbox is checked.




Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ include file="Connection1.jsp" %>
  3. <HTML>
  4.   <HEAD>
  5.  
  6.         <!-- Comments : To include style sheet </link> tag is used -->
  7.         <link rel="stylesheet" type="text/css" href="commonstyle.css" >
  8.  
  9.  
  10.       <TITLE> report </TITLE>
  11.   <script language=javascript>
  12.  
  13.     function check()
  14.  
  15.     { 
  16.  
  17.            no_of_row = document.form1.dev.value 
  18.  
  19.           //  alert(no_of_row);
  20.  
  21.            for(l=0;l<no_of_row;l++)
  22.  
  23.           {
  24.  
  25.  
  26.            if(document.form1.elements["Chk"+l].checked ==true)
  27.            {
  28.  
  29.              var v=document.form1.elements[l].value;
  30.           alert(v);
  31.  
  32.              return true;
  33.  
  34.            }
  35.  
  36.            else
  37.  
  38.            {
  39.  
  40.  
  41.  
  42.           alert('Please check the assign token');
  43.                return false;
  44.            } 
  45.  
  46.  
  47.  
  48.     }
  49.  
  50.   }
  51.   </script>
  52.  
  53.   </HEAD>
  54.   <BODY>
  55. <% 
  56. try
  57. {
  58.          com.aspace.ftress.interfaces.ejb.impl.BIAuthenticator ftressAuthenticator = soapFactory.getAuthenticatorEJB();
  59.          com.aspace.ftress.interfaces.ejb.impl.BIUserManager ftressUserManager = soapFactory.getUserManagerEJB();
  60.  
  61.              //Defining the attribute search criteria. This is the input parameter for userSearchCriteria
  62.              Attribute[] att_SearchCriteria = new Attribute[1];
  63.              att_SearchCriteria[0] = new Attribute();
  64.              att_SearchCriteria[0].setTypeCode(new AttributeTypeCode("GID"));
  65.              att_SearchCriteria[0].setValue("infy");
  66.  
  67.              //Defining the user search criteria. This is the input parameter for the searchUser method
  68.              UserSearchCriteria userSearchCriteria = new UserSearchCriteria();
  69.              userSearchCriteria.setAttributeCriteria(att_SearchCriteria);
  70.  
  71.              //searchUser method returns all users matching the UserSearchCriteria.
  72.              UserSearchResults userSearchResults =  new UserSearchResults();
  73.              userSearchResults = ftressUserManager.searchUsers(alsi,channelCode,userSearchCriteria, domain);
  74.  
  75.              //Retrieving the users from the userSearchResults object.
  76.              User[] user_Arr = userSearchResults.getUsers();
  77.              int user_ArrLen = user_Arr.length;
  78.  
  79.              //Displaying the user attributes - START
  80.              //Iterating through each user object in order to access User Attributes.
  81.              String userCode = null;
  82.              Attribute[] user_AttArray = new Attribute[10];
  83.              boolean flag=true;
  84.              boolean flag1=true;
  85.              int i=0;
  86.  
  87.              %>
  88.  
  89.              <form name="form1" action="test.jsp" onSubmit=" return check()">
  90.              <table border=8 width="50% height="80%" align="center">
  91.  
  92.                     <tr><td colspan="9" align= "center"><h2>USER SEARCH </h2></td></tr>
  93.              <td>EXTID</td>
  94.  
  95.                 <%
  96.  
  97.  
  98.              String[] arr_TextValues = new String[10];
  99.  
  100.              String[] arr_ChkValues = new String[10];
  101.  
  102.              for (i=0; i<user_ArrLen; i++ ) {
  103.  
  104.              userCode = (user_Arr[i].getCode()).getCode();
  105.  
  106.              //Accessing attributes of the User
  107.  
  108.              user_AttArray = user_Arr[i].getAttributes();
  109.  
  110.               if(flag==true)
  111.  
  112.               for(int k=0;k<user_AttArray.length;k++){
  113.  
  114.                  %>
  115.  
  116.              <td><%=(user_AttArray[k].getTypeCode()).getCode() %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  117.  
  118.                         <%
  119.                              flag=false;
  120.                          }
  121.  
  122.                         if(flag1==true){
  123.  
  124.                         %>
  125.  
  126.              <td>Token Serial No.</td>
  127.                     <td>Assign Token</td>
  128.  
  129.              </tr>
  130.  
  131.              <%flag1=false;
  132.              }
  133.              %>
  134.                  <tr>
  135.  
  136.  
  137.               <td><%=userCode%></td>
  138.  
  139.              <%
  140.  
  141.              //Iterating to access the value of each user attribute
  142.  
  143.                for (int j=0;j<user_AttArray.length;j++){
  144.                %>
  145.                <td><%=(user_AttArray[j].getValue())%></td>
  146.  
  147.                <%
  148.  
  149.                  }
  150.  
  151.                %>
  152.                  <% 
  153.  
  154.  
  155.  
  156.                    arr_TextValues[i] = "Text"+i;
  157.                    arr_ChkValues[i] = "Chk"+i;
  158.                    out.println(arr_TextValues[i]);
  159.                    out.println(arr_ChkValues[i]);
  160.  
  161.                  %>
  162.  
  163.                <td><input type=text id=<%=arr_TextValues[i]%> value=""/></td>
  164.  
  165.                   <td><input type="checkbox" id=<%=arr_TextValues[i]%> VALUE="0" align ="center" ></td> 
  166.  
  167.                   <%
  168.              }
  169.              %>
  170.              <%
  171.              String str_no_of_elements = Integer.toString(i);
  172.  
  173.             //out.println(str_no_of_elements);
  174.  
  175.             session.setAttribute("str_no", str_no_of_elements);
  176.  
  177.              %>
  178.  
  179.  
  180.  
  181.  
  182.  
  183.              <tr><td colspan="9" align = "center"><input type=button name=reg value="Submit" onClick=check() >
  184.             <input type=button name=reg1 value="Cancel" align = "center"  >
  185.             <input type=hidden name=dev value="<%=str_no_of_elements%>"</td></tr>
  186.                 </form>
  187.              <%
  188.  
  189.  
  190.              //Displaying the user attributes - END
  191.  
  192.  
  193.  
  194.          //Logging out the ALSI object
  195.          //ftressAuthenticator.logout(response.getAlsi(), channelCode, domain);
  196.  
  197.          //Exception Handling
  198.  
  199.                 } catch(com.aspace.ftress.interfaces.ftress.DTO.exception.ObjectNotFoundException e) {
  200.              e.printStackTrace();
  201.          } catch(javax.naming.NamingException e) {
  202.              e.printStackTrace();
  203.          } catch(javax.ejb.CreateException e) {
  204.              e.printStackTrace();
  205.          } catch(java.rmi.RemoteException e) {
  206.              e.printStackTrace();
  207.          } catch(java.net.MalformedURLException e) {
  208.              e.printStackTrace();
  209.          } catch(javax.xml.rpc.ServiceException e) {
  210.              e.printStackTrace();
  211.          } catch(Exception e) {
  212.              e.printStackTrace();
  213.          }
  214.  
  215.  
  216.     %>
  217.  
  218. </table>
  219. </BODY>
  220. </HTML>
  221.  
  222.  
I will be thankful if you can help me out .
May 22 '08 #6

acoder
Expert Mod 15k+
P: 16,027
Now that you've set the ID, the following should work:
Expand|Select|Wrap|Line Numbers
  1. for(l=0;l<no_of_row;l++) {
  2.     if (document.getElementById("Chk"+l).checked) {
  3.         var v = document.getElementById("Text"+l).value;
  4.         // v now contains the text box value
  5.  
May 22 '08 #7

P: 12
Hi a have come up with the following code :
i am now storing the name value of checkbox into a variable and passing it in
Expand|Select|Wrap|Line Numbers
  1. document.form1.chk.checked ==true
but it is not taking the value of var . could you please tell me how to do this ?

Expand|Select|Wrap|Line Numbers
  1. function check()
  2.  
  3.     { 
  4.  
  5.            no_of_row = document.form1.dev.value 
  6.  
  7.           //  alert(no_of_row);
  8.  
  9.            for(l=0;l<no_of_row;l++)
  10.  
  11.           {
  12.  
  13.            var txt="Text"+l; alert(txt);
  14.            var chk="Chk"+l; alert(chk);}
  15.  
  16.       if(document.form1.chk.checked ==true)
  17.            {
  18.  
  19.              var v=document.form1.txt.value;
  20.           if(v!="")
  21.           alert(v);
  22.           else
  23.           alert("Please enter id");
  24.  
  25.              return true;
  26.  
  27.            }
  28.  
  29.            else
  30.  
  31.            {
  32.  
  33.  
  34.  
  35.           alert('Please check the assign token');
  36.                return false;
  37.            } 
  38.  
  39.  
  40.  
  41.     } 
  42.  
  43.   }
thanks!
May 23 '08 #8

P: 12
Hi all,
I have to pass the value of a variable into the following code :-

Expand|Select|Wrap|Line Numbers
  1. function check()
  2.  
  3.     { 
  4.  
  5.            no_of_row = document.form1.dev.value 
  6.  
  7.           //  alert(no_of_row);
  8.  
  9.            for(l=0;l<no_of_row;l++)
  10.  
  11.           {
  12.  
  13.            var txt="Text"+l; 
  14.            var chk="Chk"+l;
  15. }
  16.  
  17.       if(document.form1.chk.checked ==true)
  18.            {
  19.  
  20.              var v=document.form1.txt.value;
  21.           if(v!="")
  22.           alert(v);
  23.           else
  24.           alert("Please enter id");
  25.  
  26.              return true;
  27.  
  28.            }
  29.  
  30.            else
  31.  
  32.            {
  33.  
  34.  
  35.  
  36.           alert('Please check the assign token');
  37.                return false;
  38.            } 
  39.  
  40.  
  41.  
  42.     } 
  43.  
  44.   }
  45.  
There is an error that
Expand|Select|Wrap|Line Numbers
  1. document.form1.chk.checked ==true
is null or not an object.
this means it is not taking the value of the variable "chk". does anyone know how to pass the value of var into
Expand|Select|Wrap|Line Numbers
  1. document.form1.chk.checked ==true
Any help in this regard will be appriciated .
thanks!
May 23 '08 #9

hsriat
Expert 100+
P: 1,654
Can you show the HTML part, the one you get from view source?
May 23 '08 #10

P: 12
hi Hsriat,
Thanks for your reply !!
Below is the source code:
Expand|Select|Wrap|Line Numbers
  1.   <HTML>
  2.   <HEAD>
  3.  
  4.         <!-- Comments : To include style sheet </link> tag is used -->
  5.         <link rel="stylesheet" type="text/css" href="commonstyle.css" >
  6.  
  7.  
  8.       <TITLE> report </TITLE>
  9.   <script language=javascript>
  10.  
  11.     function check()
  12.  
  13.     { 
  14.  
  15.            no_of_row = document.form1.dev.value 
  16.  
  17.           //  alert(no_of_row);
  18.  
  19.            for(l=0;l<no_of_row;l++)
  20.  
  21.           {
  22.  
  23.            var txt="Text"+l; 
  24.            alert(txt);
  25.            var chk="Chk"+l; 
  26.            alert(chk);
  27.            }
  28.  
  29.       if(document.form1.chk.checked ==true)
  30.            {
  31.  
  32.              var v=document.form1.txt.value;
  33.           if(v!="")
  34.           alert(v);
  35.           else
  36.           alert("Please enter id");
  37.  
  38.              return true;
  39.  
  40.            }
  41.  
  42.            else
  43.  
  44.            {
  45.  
  46.  
  47.  
  48.           alert('Please check the assign token');
  49.                return false;
  50.            } 
  51.  
  52.  
  53.  
  54.     } 
  55.  
  56.   }
  57.   </script>
  58.  
  59.   </HEAD>
  60.   <BODY>
  61.  
  62.  
  63.              <form name="form1" action="test.jsp" onSubmit=" return check()">
  64.              <table border=8 width="50% height="80%" align="center">
  65.  
  66.                     <tr><td colspan="9" align= "center"><h2>USER SEARCH </h2></td></tr>
  67.              <td>EXTID</td>
  68.  
  69.  
  70.  
  71.              <td>GID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  72.  
  73.  
  74.  
  75.              <td>TITLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  76.  
  77.  
  78.  
  79.              <td>LASTNAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  80.  
  81.  
  82.  
  83.              <td>FIRSTNAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  84.  
  85.  
  86.  
  87.              <td>PORTAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  88.  
  89.  
  90.  
  91.              <td>COMPANY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
  92.  
  93.  
  94.  
  95.              <td>Token Serial No.</td>
  96.                     <td>Assign Token</td>
  97.  
  98.              </tr>
  99.  
  100.  
  101.                  <tr>
  102.  
  103.  
  104.               <td>User01</td>
  105.  
  106.  
  107.                <td>infy</td>
  108.  
  109.  
  110.                <td>Amitabh</td>
  111.  
  112.  
  113.                <td>Bacchan</td>
  114.  
  115.  
  116.                <td>Amitabh</td>
  117.  
  118.  
  119.                <td>null</td>
  120.  
  121.  
  122.                <td>infosys</td>
  123.  
  124.  
  125.                  Text0
  126. Chk0
  127.  
  128.  
  129.                <td><input type=text name=Text0 value=""/></td>
  130.  
  131.                   <td><input type="checkbox" name=Text0 VALUE="0" align ="center" ></td> 
  132.  
  133.  
  134.                  <tr>
  135.  
  136.  
  137.               <td>User02</td>
  138.  
  139.  
  140.                <td>infy</td>
  141.  
  142.  
  143.                <td>Jaya</td>
  144.  
  145.  
  146.                <td>Bacchan</td>
  147.  
  148.  
  149.                <td>Jaya</td>
  150.  
  151.  
  152.                <td>null</td>
  153.  
  154.  
  155.                <td>infosys</td>
  156.  
  157.  
  158.                  Text1
  159. Chk1
  160.  
  161.  
  162.                <td><input type=text name=Text1 value=""/></td>
  163.  
  164.                   <td><input type="checkbox" name=Text1 VALUE="0" align ="center" ></td> 
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.              <tr><td colspan="9" align = "center"><input type=button name=reg value="Submit" onClick=check() >
  174.             <input type=button name=reg1 value="Cancel" align = "center"  >
  175.             <input type=hidden name=dev value="2"</td></tr>
  176.                 </form>
  177.  
  178.  
  179. </table>
  180. </BODY>
  181. </HTML>
  182.  
May 23 '08 #11

hsriat
Expert 100+
P: 1,654
Do you see anything like Chk0 and Chk1 in your HTML?

By mistake you have written Text instead of Chk in the checkbox input.
May 23 '08 #12

hsriat
Expert 100+
P: 1,654
Don't use language attribute in script tag. Instead, use type="text/javascript" and change your javascript like this
Expand|Select|Wrap|Line Numbers
  1. function check() {
  2.  
  3.     no_of_row = document.form1.dev.value
  4.  
  5.     //alert(no_of_row);
  6.  
  7.     for(l=0;l<no_of_row;l++) {
  8.  
  9.         var txt="Text"+l;
  10.         //alert(txt);
  11.         var chk="Chk"+l;
  12.         //alert(chk);
  13.         //}
  14.  
  15.         if (document.form1.elements[chk].checked ==true) {
  16.  
  17.             var v=document.form1.elements[txt].value;
  18.             if(v!="")
  19.             alert(v);
  20.             else
  21.             alert("Please enter id");
  22.  
  23.             return true;
  24.  
  25.         }
  26.  
  27.            else {
  28.             alert('Please check the assign token');
  29.             return false;
  30.            }
  31.     }
  32. }
And it seems you are a great fan of Amitabh.

Regards
May 23 '08 #13

acoder
Expert Mod 15k+
P: 16,027
Hi a have come up with the following code :
i am now storing the name value of checkbox into a variable and passing it in
Expand|Select|Wrap|Line Numbers
  1. document.form1.chk.checked ==true
but it is not taking the value of var . could you please tell me how to do this ?
It won't work like that because 'chk' is a variable containing the checkbox name, not the name of the checkbox. If you're going back to using the name, use the elements array:
Expand|Select|Wrap|Line Numbers
  1. document.form1.elements[chk].checked 
If that still doesn't work, post the code as it appears in the browser (view source).
May 23 '08 #14

acoder
Expert Mod 15k+
P: 16,027
Threads merged. Please do not double post.

Moderator.
May 23 '08 #15

P: 12
Hi all,
I have to pass the variable value in my javascript code, does anyone know how to do this ?
The code is as-
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. for(l=0;l<no_of_row;l++)
  4.  
  5. {
  6. var txt="Text"+l; 
  7. var chk="Chk"+l; 
  8.  
  9. var checkbox = eval("document.form1."+chk+".value");
  10. if(document.form1.checkbox.checked ==true)
  11.            {
  12. alert(checkbox); }
  13. }
  14.  
I am getting the value of checkbox but when i am passing it into
Expand|Select|Wrap|Line Numbers
  1. if(document.form1.checkbox.checked ==true)
  2.  
it is returning 0 irrespective of whether the checkbox is checked or not.

Any pointers ??
Thanks in advance!!
May 23 '08 #16

hsriat
Expert 100+
P: 1,654
Dude... ... are you even looking at the posts above? or you just hate reading like I do?

Go through the posts above.. I guess I solved your problem, may be..

And what's this on line 7 in the above code. Look above, someone told you its not going to work like this, and showed you the right path too.. you are rather confusing your situation by introducing this eval. Just use elements[]

I guess you googled a lot to find the solution and ended up with an eval?.. Is it?

Good Luck...
May 23 '08 #17

acoder
Expert Mod 15k+
P: 16,027
Threads merged yet again. Do NOT double post your questions! This is the last time you'll be told nicely.

Moderator.
May 23 '08 #18

Post your reply

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