How to pass the checkbox value in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshay01
    New Member
    • May 2008
    • 12

    How to pass the checkbox value in javascript

    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
    Code:
    String[] arr_TextValues = new String[10];
    String[] arr_ChkValues = new String[10]; 
    
    arr_TextValues[i] = "Text"+i;
    arr_ChkValues[i] = "Chk"+i;
    where i = number of rows retrieved.

    Code:
    <td><input type=text name=<%=arr_TextValues[i]%> value=""/></td>
    <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 :-

    Code:
    function check()
        { 
                    for(l=0;l< i;l++){
                	
     if(document.form1.chk0.checked ==true)
        	{
        	
               	something to retrieve the value of textbox and pass it to another jsp!!!  	     
                    
        	
                   }
    }
    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 .
  • akshay01
    New Member
    • May 2008
    • 12

    #2
    How to pass the value of i ????

    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 :

    Code:
    function check()
        { 
             for(i=0;i< 10 ;i++){
            
        	if(document.form1.chki.checked ==true)
        	{
        	  alert("hi");
                    }
       }
    
    }
    but it is now taking the value of "i" in (document.form1 .chki.checked ==true).
    Thanks in advance .

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Welcome to Bytes.

      To take the i value to use for the name of the checkbox, use the elements array:
      [code=javascript]document.form1. elements["chk"+i].checked...[/code]

      PS. merged threads.

      Comment

      • akshay01
        New Member
        • May 2008
        • 12

        #4
        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

        Code:
        function check()
            
            { 
                no_of_row = document.form1.dev.value 
                                 
                  for(l=0;l<no_of_row;l++)
                  
                  {
               	if(document.form1.elements["Chk"+l].checked ==true)
               	{
               	  
               	  var v=document.form1.elements[l].value;
                          alert(v);
                       
               	  return true;
               	
               	}
               	
               	else
               	
               	{
               	
               	
         
                  alert('Please check the assign token');
               		return false;
               	} 
            
            
             
            }
            
          }
        please suggest me what to do now .

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          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.

          Comment

          • akshay01
            New Member
            • May 2008
            • 12

            #6
            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.




            Code:
            <%@ include file="Connection1.jsp" %>
            <HTML>
              <HEAD>
            
                	<!-- Comments : To include style sheet </link> tag is used -->
                	<link rel="stylesheet" type="text/css" href="commonstyle.css" >
            
            	
              	<TITLE> report </TITLE>
              <script language=javascript>
                
                function check()
                
                { 
               
                       no_of_row = document.form1.dev.value 
                      
                      //  alert(no_of_row);
                      
                       for(l=0;l<no_of_row;l++)
                       
                      {
                       
                              	
                   	if(document.form1.elements["Chk"+l].checked ==true)
                   	{
                   	  
                   	  var v=document.form1.elements[l].value;
                      alert(v);
                           
                   	  return true;
                   	
                   	}
                   	
                   	else
                   	
                   	{
                   	
                   	
             
                      alert('Please check the assign token');
                   		return false;
                   	} 
                
                
                 
                }
                
              }
              </script>
            
              </HEAD>
              <BODY>
            <% 
            try
            {
                     com.aspace.ftress.interfaces.ejb.impl.BIAuthenticator ftressAuthenticator = soapFactory.getAuthenticatorEJB();
                     com.aspace.ftress.interfaces.ejb.impl.BIUserManager ftressUserManager = soapFactory.getUserManagerEJB();
             
             			//Defining the attribute search criteria. This is the input parameter for userSearchCriteria
             			Attribute[] att_SearchCriteria = new Attribute[1];
             			att_SearchCriteria[0] = new Attribute();
             			att_SearchCriteria[0].setTypeCode(new AttributeTypeCode("GID"));
             			att_SearchCriteria[0].setValue("infy");
             
             			//Defining the user search criteria. This is the input parameter for the searchUser method
             			UserSearchCriteria userSearchCriteria = new UserSearchCriteria();
             			userSearchCriteria.setAttributeCriteria(att_SearchCriteria);
             
             			//searchUser method returns all users matching the UserSearchCriteria.
             			UserSearchResults userSearchResults =  new UserSearchResults();
             			userSearchResults = ftressUserManager.searchUsers(alsi,channelCode,userSearchCriteria, domain);
             
             			//Retrieving the users from the userSearchResults object.
             			User[] user_Arr = userSearchResults.getUsers();
             			int user_ArrLen = user_Arr.length;
             
             			//Displaying the user attributes - START
             			//Iterating through each user object in order to access User Attributes.
             			String userCode = null;
             			Attribute[] user_AttArray = new Attribute[10];
             			boolean flag=true;
             			boolean flag1=true;
             			int i=0;
             			
             			%>
             			
             			<form name="form1" action="test.jsp" onSubmit=" return check()">
             			<table border=8 width="50% height="80%" align="center">
            				
            	                <tr><td colspan="9" align= "center"><h2>USER SEARCH </h2></td></tr>
             			<td>EXTID</td>
             		       
             		       <%
             			
             			
             			String[] arr_TextValues = new String[10];
             			
             			String[] arr_ChkValues = new String[10];
             			
             			for (i=0; i<user_ArrLen; i++ ) {
             
             			userCode = (user_Arr[i].getCode()).getCode();
             			
             			//Accessing attributes of the User
             			
             			user_AttArray = user_Arr[i].getAttributes();
             			  
             			 if(flag==true)
             			  
             			 for(int k=0;k<user_AttArray.length;k++){
             		        
             		        %>
             			
             			<td><%=(user_AttArray[k].getTypeCode()).getCode() %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
             		
                                    <%
                                         flag=false;
                                     }
                                    
                                    if(flag1==true){
                                    
                                    %>
             			
             			<td>Token Serial No.</td>
            	                <td>Assign Token</td>
             			
             			</tr>
             			
             			<%flag1=false;
             			}
             			%>
             		        <tr>
             			
             			
             			 <td><%=userCode%></td>
             			
             			<%
             			
             			//Iterating to access the value of each user attribute
             			  
             			  for (int j=0;j<user_AttArray.length;j++){
             			  %>
             			  <td><%=(user_AttArray[j].getValue())%></td>
             				
             			  <%
             			    
             			    }
             			    
             			  %>
             			    <% 
             			      
             			      
             			      
             			      arr_TextValues[i] = "Text"+i;
             			      arr_ChkValues[i] = "Chk"+i;
             			      out.println(arr_TextValues[i]);
             			      out.println(arr_ChkValues[i]);
             			      
             			    %>
             			 
             			  <td><input type=text id=<%=arr_TextValues[i]%> value=""/></td>
            		          
            		          <td><input type="checkbox" id=<%=arr_TextValues[i]%> VALUE="0" align ="center" ></td> 
            		          
            		          <%
             			}
             			%>
             			<%
             			String str_no_of_elements = Integer.toString(i);
            				
            			//out.println(str_no_of_elements);
            				
            			session.setAttribute("str_no", str_no_of_elements);
             			
             			%>
             			
             			
             			
             			
             		        
             			<tr><td colspan="9" align = "center"><input type=button name=reg value="Submit" onClick=check() >
            			<input type=button name=reg1 value="Cancel" align = "center"  >
            			<input type=hidden name=dev value="<%=str_no_of_elements%>"</td></tr>
            		        </form>
             			<%
             				
             			
             			//Displaying the user attributes - END
             
             
             
             		//Logging out the ALSI object
             		//ftressAuthenticator.logout(response.getAlsi(), channelCode, domain);
             
             		//Exception Handling
             		
                            } catch(com.aspace.ftress.interfaces.ftress.DTO.exception.ObjectNotFoundException e) {
             			e.printStackTrace();
             		} catch(javax.naming.NamingException e) {
             			e.printStackTrace();
             		} catch(javax.ejb.CreateException e) {
             			e.printStackTrace();
             		} catch(java.rmi.RemoteException e) {
             			e.printStackTrace();
             		} catch(java.net.MalformedURLException e) {
             			e.printStackTrace();
             		} catch(javax.xml.rpc.ServiceException e) {
             			e.printStackTrace();
             		} catch(Exception e) {
             			e.printStackTrace();
             		}
                 
            
            	%>
            	
            </table>
            </BODY>
            </HTML>
            I will be thankful if you can help me out .

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Now that you've set the ID, the following should work:
              [code=javascript]for(l=0;l<no_of _row;l++) {
              if (document.getEl ementById("Chk" +l).checked) {
              var v = document.getEle mentById("Text" +l).value;
              // v now contains the text box value
              [/code]

              Comment

              • akshay01
                New Member
                • May 2008
                • 12

                #8
                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
                Code:
                document.form1.chk.checked ==true
                but it is not taking the value of var . could you please tell me how to do this ?

                Code:
                function check()
                   
                    { 
                 
                           no_of_row = document.form1.dev.value 
                          
                          //  alert(no_of_row);
                          
                           for(l=0;l<no_of_row;l++)
                           
                          {
                           
                           var txt="Text"+l; alert(txt);
                           var chk="Chk"+l; alert(chk);}
                      
                      if(document.form1.chk.checked ==true)
                       	{
                       	  
                       	  var v=document.form1.txt.value;
                		  if(v!="")
                          alert(v);
                		  else
                		  alert("Please enter id");
                               
                       	  return true;
                       	
                       	}
                       	
                       	else
                       	
                       	{
                       	
                       	
                 
                          alert('Please check the assign token');
                       		return false;
                       	} 
                    
                    
                     
                    } 
                    
                  }
                thanks!

                Comment

                • akshay01
                  New Member
                  • May 2008
                  • 12

                  #9
                  How to pass the variable value in document.form1. chk.checked

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

                  Code:
                  function check()
                      
                      { 
                     
                             no_of_row = document.form1.dev.value 
                            
                            //  alert(no_of_row);
                            
                             for(l=0;l<no_of_row;l++)
                             
                            {
                             
                             var txt="Text"+l; 
                             var chk="Chk"+l;
                  }
                        
                        if(document.form1.chk.checked ==true)
                         	{
                         	  
                         	  var v=document.form1.txt.value;
                  		  if(v!="")
                            alert(v);
                  		  else
                  		  alert("Please enter id");
                                 
                         	  return true;
                         	
                         	}
                         	
                         	else
                         	
                         	{
                         	
                         	
                   
                            alert('Please check the assign token');
                         		return false;
                         	} 
                      
                      
                       
                      } 
                      
                    }
                  There is an error that
                  Code:
                  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
                  Code:
                  document.form1.chk.checked ==true
                  Any help in this regard will be appriciated .
                  thanks!

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Can you show the HTML part, the one you get from view source?

                    Comment

                    • akshay01
                      New Member
                      • May 2008
                      • 12

                      #11
                      hi Hsriat,
                      Thanks for your reply !!
                      Below is the source code:
                      Code:
                        <HTML>
                        <HEAD>
                      
                          	<!-- Comments : To include style sheet </link> tag is used -->
                          	<link rel="stylesheet" type="text/css" href="commonstyle.css" >
                      
                      	
                        	<TITLE> report </TITLE>
                        <script language=javascript>
                          
                          function check()
                          
                          { 
                         
                                 no_of_row = document.form1.dev.value 
                                
                                //  alert(no_of_row);
                                
                                 for(l=0;l<no_of_row;l++)
                                 
                                {
                                 
                                 var txt="Text"+l; 
                                 alert(txt);
                                 var chk="Chk"+l; 
                                 alert(chk);
                                 }
                            
                            if(document.form1.chk.checked ==true)
                             	{
                             	  
                             	  var v=document.form1.txt.value;
                      		  if(v!="")
                                alert(v);
                      		  else
                      		  alert("Please enter id");
                                     
                             	  return true;
                             	
                             	}
                             	
                             	else
                             	
                             	{
                             	
                             	
                       
                                alert('Please check the assign token');
                             		return false;
                             	} 
                          
                          
                           
                          } 
                          
                        }
                        </script>
                      
                        </HEAD>
                        <BODY>
                      
                       			
                       			<form name="form1" action="test.jsp" onSubmit=" return check()">
                       			<table border=8 width="50% height="80%" align="center">
                      				
                      	                <tr><td colspan="9" align= "center"><h2>USER SEARCH </h2></td></tr>
                       			<td>EXTID</td>
                       		       
                       		       
                       			
                       			<td>GID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>TITLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>LASTNAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>FIRSTNAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>PORTAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>COMPANY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                       		
                                              
                       			
                       			<td>Token Serial No.</td>
                      	                <td>Assign Token</td>
                       			
                       			</tr>
                       			
                       			
                       		        <tr>
                       			
                       			
                       			 <td>User01</td>
                       			
                       			
                       			  <td>infy</td>
                       				
                       			  
                       			  <td>Amitabh</td>
                       				
                       			  
                       			  <td>Bacchan</td>
                       				
                       			  
                       			  <td>Amitabh</td>
                       				
                       			  
                       			  <td>null</td>
                       				
                       			  
                       			  <td>infosys</td>
                       				
                       			  
                       			    Text0
                      Chk0
                      
                       			 
                       			  <td><input type=text name=Text0 value=""/></td>
                      		          
                      		          <td><input type="checkbox" name=Text0 VALUE="0" align ="center" ></td> 
                      		          
                      		          
                       		        <tr>
                       			
                       			
                       			 <td>User02</td>
                       			
                       			
                       			  <td>infy</td>
                       				
                       			  
                       			  <td>Jaya</td>
                       				
                       			  
                       			  <td>Bacchan</td>
                       				
                       			  
                       			  <td>Jaya</td>
                       				
                       			  
                       			  <td>null</td>
                       				
                       			  
                       			  <td>infosys</td>
                       				
                       			  
                       			    Text1
                      Chk1
                      
                       			 
                       			  <td><input type=text name=Text1 value=""/></td>
                      		          
                      		          <td><input type="checkbox" name=Text1 VALUE="0" align ="center" ></td> 
                      		          
                      		          
                       			
                       			
                       			
                       			
                       			
                       		        
                       			<tr><td colspan="9" align = "center"><input type=button name=reg value="Submit" onClick=check() >
                      			<input type=button name=reg1 value="Cancel" align = "center"  >
                      			<input type=hidden name=dev value="2"</td></tr>
                      		        </form>
                       			
                      	
                      </table>
                      </BODY>
                      </HTML>

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Do you see anything like Chk0 and Chk1 in your HTML?

                        By mistake you have written Text instead of Chk in the checkbox input.

                        Comment

                        • hsriat
                          Recognized Expert Top Contributor
                          • Jan 2008
                          • 1653

                          #13
                          Don't use language attribute in script tag. Instead, use type="text/javascript" and change your javascript like this
                          [code=javascript]function check() {

                          no_of_row = document.form1. dev.value

                          //alert(no_of_row );

                          for(l=0;l<no_of _row;l++) {

                          var txt="Text"+l;
                          //alert(txt);
                          var chk="Chk"+l;
                          //alert(chk);
                          //}

                          if (document.form1 .elements[chk].checked ==true) {

                          var v=document.form 1.elements[txt].value;
                          if(v!="")
                          alert(v);
                          else
                          alert("Please enter id");

                          return true;

                          }

                          else {
                          alert('Please check the assign token');
                          return false;
                          }
                          }
                          }[/code]

                          And it seems you are a great fan of Amitabh.

                          Regards

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by akshay01
                            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
                            Code:
                            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:
                            Code:
                            document.form1.elements[chk].checked
                            If that still doesn't work, post the code as it appears in the browser (view source).

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Threads merged. Please do not double post.

                              Moderator.

                              Comment

                              Working...