SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how many checkboxes?

    I have 2 checkboxes with the same name and different values. I have a validation script which checks various form entries (it can be used on any form). I am trying to check that at least one of the values has been checked.

    Code:
    if(type=='tf'){
    		var oneChecked=false
    		for(i=0;i<document.getElementById(objName).length;i++){
    			if(document.getElementById(objName)[i].checked==true){
    				oneChecked=true
    				break;
    			}
    		}
    		if(oneChecked==false){
    			errors=errors+label+' must be selected\n';
    			return;
    		}
    	}
    Here is the section of code to do this. For some reason the length is comeing back as undefined. I have tested document.getElementById(objName) and it comes back as an object.

    What is going wrong?

    cheers

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can iterate through the checkboxes with the same name using getElementsByName. You are now using getElementById, which will get a specific node matching that id. Ids are unique, so you shouldn't have more than one element with the same id.

  3. #3
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    You can iterate through the checkboxes with the same name using getElementsByName. You are now using getElementById, which will get a specific node matching that id. Ids are unique, so you shouldn't have more than one element with the same id.
    sorted! Cheers
    monkey - the rest is history


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •