473,544 Members | 1,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass the checkbox value in javascript

12 New Member
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
17 19096
akshay01
12 New Member
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
16,027 Recognized Expert Moderator MVP
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
akshay01
12 New Member
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
16,027 Recognized Expert Moderator MVP
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.getEle mentById("Chk0" ) to access it.
May 22 '08 #5
akshay01
12 New Member
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
16,027 Recognized Expert Moderator MVP
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
akshay01
12 New Member
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
akshay01
12 New Member
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
1,654 Recognized Expert Top Contributor
Can you show the HTML part, the one you get from view source?
May 23 '08 #10

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

Similar topics

0
2115
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to construct a checkbox array in a survey form where one of the choices is "No Preference" which is checked by default. If the victim chooses other than "No...
3
6778
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little stuck: I have page of results obtained from a MySQL query presented as a table (the first column having checkboxes for each of the rows in the table,...
4
5373
by: Fred | last post by:
Hi, i know how to pass a value from Javascript to ASP with a hidden field into a form and submitting it, or with cookies, but here i have to pass a lot of data in an array. There is a list of product the visitor can order by clicking one or more checkboxes. I made a form containing input with type "checkbox" like: <form> <input...
1
2191
by: jaya | last post by:
Hi, I am using asp.net to pass parameters from .aspx page to my Microsoft reporting service. My .aspx page has checkbox. If it is checked it the chekbox should pass value =0 to my report <INPUT id="ReviewedBy" style="WIDTH: 16px; HEIGHT: 20px" type="checkbox" name="ReviewedBy" value = 0> if the check box is unchecked it should pass value...
3
4482
by: Marc Castrechini | last post by:
I have a page that changes an <ASP:Checkbox value based on a user entered value in a textboxm using client side Javascript. After my submit is fired the value for the chkMyCB.checked does not get the latest value of the checkbox. Does this sound like a posibility and if so how can I force the server to get the new value from the page? ...
5
3661
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.... 2. after population there should be validation in checkboxlist..... that is if user clicks a button wihout checking any one of them(i.e. at least...
34
3750
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript function needs to run and validate all the checkboxes on my form and make sure none of them are unchecked. I suck at Javascript and my problem is...
6
10386
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to group by using a two dimensional array. <form name=msgs>
5
17149
by: Muffinthief | last post by:
Hi guys, what I'm trying to do is very simple. Just make a simple check/uncheck button for some checkboxes. Here's what I have. <script language="JavaScript"> function checkAll(field) { for (i = 0; i < field.length; i++) field.checked = true ; } function uncheckAll(field) {
0
7439
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7785
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5935
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
1851
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
997
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
679
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.