SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation - dynamic checkbox

    Hi,

    I have a form which contains a number of check boxes in format of fielduse_x where x is incremented each time (1 - 30)

    Everytime one of the checkboxes is ticked or unticked i want to perform a function for related boxes but i am having difficaulty doing this.

    The code i have in the javascript is :

    function changeStatus(f)
    {
    alert(f);
    if(document.newForm.fielduse_[f].checked==true)
    alert("YES")
    else
    alert("NO");
    }

    f is passed from the form field and contains the number (1 - 30). I know that the form is passing the values across as alert(f) gives the correct number. I don't seem to be able to get the next line to work. How do i put the value of f into the if statment as what i have done isn't working

    if(document.newForm.fielduse_[f].checked==true)

    Thanks

    K
    Eternity

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is your checkbox input name follow the your naming standard as described by you, name="fielduse_[n]", n == number

  3. #3
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is
    Eternity

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Browsers think that you have an group of form controls named "fielduse_".

    You need to use some array syntax instead of all dot syntax. (Also since the checked property is either true or false, you don't need to check if it's equal to true.)

    Code:
    if(document.newForm.elements['fielduse_['+f+']'].checked)
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kravvitz.

    That almost worked - just had to change to
    document.newForm.elements['fielduse_'+ f].checked

    Cheers
    Eternity

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So your answer to SimplyFu was incorrect then?
    Ian Anderson
    www.siteguru.co.uk


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
  •