SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checkboxes id name and for loop

    Code JavaScript:
    	checkTitle=document.getElementsByName('checkTitle');
    	for (i=0;i<checkTitle.length;i++){
    		CheckTheTitle=document.getElementById('CheckTheTitle'+(i+1));
    		if (CheckTheTitle[(i+1)].checked){
    			getKeyURL=getKeyURL + "&checkTitle"+i+"=" + CheckTheTitle[i].value;
    		}
    	}
    Suppose I have 2 checked out of 66 books.
    Code HTML4Strict:
    <input name="checkTitle" id="CheckTheTitle1" value="book1" type="checkbox"/>book1
    ...
    <input name="checkTitle" id="CheckTheTitle21" value="book21" type="checkbox"/>book21
    I don't understand how it's going to run through the for loop.
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The loop will run fine, although the resulting query string will look like.
    Code:
    checkTitle0=book1&checkTitle20=book21
    As long as the serverside script which receives this can handle it, its not a problem. Although, you can may be able to use a different format that may be easier for your serverside script to receive and process.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may be due invalid declaration of javascript variable checkTitle declare the variable as
    Code:
    var checkTitle=document.getElementsByName('checkTitle');
    you can't execute the loop unless you get the checkTitle.length

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The loop will run fine, although the resulting query string will look like.
    Code:
    checkTitle0=book1&checkTitle20=book21
    As long as the serverside script which receives this can handle it, its not a problem. Although, you can may be able to use a different format that may be easier for your serverside script to receive and process.
    I'm getting:
    Error: CheckTheTitle[i] is undefined
    Source File: http:...ajax.js
    Line: 300
    Compare bible texts (and other tools):
    TheWheelofGod

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah.

    CheckTheTitle.checked
    CheckTheTitle.value

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CheckTheTitle is not an array object so to find out whether it's checked you should be doing this:

    Code JavaScript:
    if (CheckTheTitle.checked){
          getKeyURL=getKeyURL + "&checkTitle"+i+"=" + CheckTheTitle[i].value;
    }

    Instead of:

    Code JavaScript:
    if (CheckTheTitle[(i+1)].checked){
          getKeyURL=getKeyURL + "&checkTitle"+i+"=" + CheckTheTitle[i].value;
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    730
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought I should be more precise and put Name and Id next to checkTitle.
    I added var j for checkTitleId to differentiate from i of checkTitleName
    Code JavaScript:
    	checkTitleName=document.getElementsByName('checkTitleName');
    	var checkTitleId = new Array();
    	for (i=0;i<checkTitleName.length;i++){
    		var j=i+1;
    		checkTitleId[j]=document.getElementById('checkTitleId'+j);
    		if (checkTitleName[i].checked){
    			getKeyURL=getKeyURL + "&checkTitleName"+i+"=" + checkTitleName[i].value;
    		}
    	}
    But the url, this time, is showing no &checkTitleName. It's not picking it up. There must be a confusion between j and i somewhere.
    Compare bible texts (and other tools):
    TheWheelofGod


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
  •