SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkbox problem when just one row left ..

    Hi friends: I am having a problem with my checkbox selection script. The following function works fine when there are more than two checkboxes with the same name. But if only one checkbox left and i try to submit my form by clicking on it. It alert "Please select the movie" although i have already selected it.


    function isMovieSelected() {

    // set var radio_choice to false
    var ckb0_choice = false;
    //alert("Movie id isl length is: " + myForm1.ckb0[counter].value)
    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < myForm1.ckb0.length; counter++)
    {

    // If a radio button has been selected it will return true

    // (If not it will return false)
    if (myForm1.ckb0[counter].checked)
    {
    //alert("Movie id is: " + myForm1.ckb0[counter].value)
    ckb0_choice = true;
    }
    }

    if (!ckb0_choice)
    {
    // If there were no selections made display an alert box
    alert("Please select a Movie")
    return (false);
    }
    return (true);
    }

    Whats going wrong with this function. It works fine when more than one checkboxes are there. Only problem when just one checkbox left.
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  2. #2
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It just giving me the length= "undefined" although one checkbox exist there.
    and It works fine and give me the length right when there are more than one checkboxes. (Note: all checkboxes are of same name, name is not the problem etc..)

    alert("checkbox length is: " + myForm1.ckb0.length)
    above alert return "undefined" when only one checkbox left. Any solution.
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  3. #3
    SitePoint Enthusiast Kisan's Avatar
    Join Date
    Jun 2003
    Location
    India
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this out.the code is not checked. Let me know if this works
    Code:
    unction isMovieSelected() 
    {
         var ckb0_choice = false;
         if(!myForm1.ckb0.length)
         {
    	 if (myForm1.ckb0[counter].checked) ckb0_choice true
         }
         else{
             for (counter = 0; counter < myForm1.ckb0.length; counter++)
             {
                if (myForm1.ckb0[counter].checked) ckb0_choice = true;
           
             }
          }
    
       if (!ckb0_choice)
       {
        alert("Please select a Movie")
        return (false);
    }
    return (true);
    }
    kisan

  4. #4
    SitePoint Enthusiast Kisan's Avatar
    Join Date
    Jun 2003
    Location
    India
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry after posting i saw an error in the code
    just remove [counter]

    if(!myForm1.ckb0.length)
    {
    if (myForm1.ckb0.checked) ckb0_choice true
    }
    kisan

  5. #5
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx Kisan for your time and support, but in my case there are multiple checkboxes with same name as "ckb0". What i am looking for is that when just one checkbox left (say i delete other rows.) then it give me the length "undefined" as
    alert("checkbox length is: " + myForm1.ckb0.length) // this gives
    // checkbox length is: undefined
    Although this is working properly with more than one rows.

    Any advice.
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  6. #6
    SitePoint Enthusiast Kisan's Avatar
    Join Date
    Jun 2003
    Location
    India
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function isMovieSelected() 
    {
         var ckb0_choice = false;
         if(!myForm1.ckb0.length)
         {
    	 if (myForm1.ckb0.checked) ckb0_choice true
         }
         else{
             for (counter = 0; counter < myForm1.ckb0.length; counter++)
             {
                if (myForm1.ckb0[counter].checked) ckb0_choice = true;
           
             }
          }
    
       if (!ckb0_choice)
       {
        alert("Please select a Movie")
        return (false);
    }
    return (true);
    }
    did you try the code?
    if there is only one checkbox then checkbox length is: undefined
    so if(!myForm1.ckb0.length) is true and it again checks
    if (myForm1.ckb0.checked) if checked then ckb0_choice true

    When there is more than one check box then the else function works.

    kisan

  7. #7
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its really working. I just checked it now. My problem has been solved.

    Thanx Kisan.

    But Kisan is it the Javascript fault that it return "undefined" instead of "1" ?
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you only have 1 checkbox, your element

    document.form.checkbox

    is a form element, not an array of form elements, so .length is not a valid property.

  9. #9
    SitePoint Enthusiast Nawaz Ijaz's Avatar
    Join Date
    Feb 2005
    Location
    Lahore, Pakistan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx i am getting now.
    Nawaz Ijaz [Lahore, Pakistan]
    Boy: He is big..i wont like to fight him...
    Achilles: Thatz Y no one wud remember u after thousand years...!!

  10. #10
    SitePoint Zealot contactnaveen's Avatar
    Join Date
    Oct 2004
    Location
    India
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also got the same problem.. Now I got it!!!! Thanx

  11. #11
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of
    Code:
    myForm1.ckb0
    you could have used

    Code:
    document.getElementsByName('ckb0')
    that still returns a collection of one lement event if you only have one element ...


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
  •