Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: does anyone see anything wrong with this code???

  1. #1
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default does anyone see anything wrong with this code???

    this code doesn't work:


    if(document.signup.checkbox1.checked)
    {document.signup.account1.disabled=false;}

    if(document.signup.checkbox2.checked)
    {document.signup.account1.disabled=false && document.signup.account2.disabled=false;}

    if(document.signup.checkbox3.checked)
    {document.signup.account1.disabled=false && document.signup.account2.disabled=false; && document.signup.account3.disabled=false;}

    if(document.signup.checkbox4.checked)
    {document.signup.account1.disabled=false && document.signup.account2.disabled=false && document.signup.account3.disabled=false && document.signup.account4.disabled=false;}

    if(document.signup.checkbox5.checked)
    {document.signup.account1.disabled=false && document.signup.account2.disabled=false && document.signup.account3.disabled=false && document.signup.account4.disabled=false && document.signup.account5.disabled=false;}

    }

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Change && to ;

    And don't assume form elements as properties of a document.
    Always use: document.forms['formname'].elements['inputname'].disabled = false
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Adding on to the above post, the code would look like so:

    Code:
    if(document.forms["signup"].elements["checkbox1"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;}
    
    if(document.forms["signup"].elements["checkbox2"].checked)
    { document.forms["signup"].elements["account1"].disabled=false;
       document.forms["signup"].elements["account2"].disabled=false;}
    
    if(document.forms["signup"].elements["checkbox3"].checked)
    {document.forms["signup"].elements["account1"].disabled=false; 
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false;}
    
    if(document.forms["signup"].elements["checkbox4"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false; 
    document.forms["signup"].elements["account4"].disabled=false;}
    
    if(document.forms["signup"].elements["checkbox5"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false;
    document.forms["signup"].elements["account4"].disabled=false;
    document.forms["signup"].elements["account5"].disabled=false;
    }
    
    }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or:
    Code:
    for(var i = 1, f = document.forms['signup'].elements, e; e = f['checkbox' + i]; ++i)
      if(e.checked)
        for(var j = 1, g; j <= i && g = f['account' + j]; ++j)
          g.disabled = false;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Thank-you, Twey. I really hate seeing repetitive code like that, when those great things called... LOOPS. Will work fine .
    - Mike

  6. #6
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you all for all the input. I really appreciate it.

    So, the code given to me by thetestingsite works. But I tried the code posted by twey, and it did not work...not sure why.

    I'm just glad it's working!

    However, I still have a problem. Let's say a person clicks checkbox5, this enables all of the text fields. And then they change their mind and click on checkbox2. What happens is, all of the text fields are still enabled.

    So basically, I want each checkbox to enable certain text fields, but if the checkbox is deselected, I want the text fields to be disabled again.

    I hope this makes sense. I wrote a code that I thought might work...but of course it doesn't. Here it is:









    {

    if(document.forms["signup"].elements["checkbox1"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;}

    else {document.forms["signup"].elements["account1"].disabled=true;}


    if(document.forms["signup"].elements["checkbox2"].checked)
    { document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;}

    else { document.forms["signup"].elements["account1"].disabled=true;
    document.forms["signup"].elements["account2"].disabled=true;}


    if(document.forms["signup"].elements["checkbox3"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false;}

    else {document.forms["signup"].elements["account1"].disabled=true;
    document.forms["signup"].elements["account2"].disabled=true;
    document.forms["signup"].elements["account3"].disabled=true;}


    if(document.forms["signup"].elements["checkbox4"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false;
    document.forms["signup"].elements["account4"].disabled=false;}

    else {document.forms["signup"].elements["account1"].disabled=true;
    document.forms["signup"].elements["account2"].disabled=true;
    document.forms["signup"].elements["account3"].disabled=true;
    document.forms["signup"].elements["account4"].disabled=true;}


    if(document.forms["signup"].elements["checkbox5"].checked)
    {document.forms["signup"].elements["account1"].disabled=false;
    document.forms["signup"].elements["account2"].disabled=false;
    document.forms["signup"].elements["account3"].disabled=false;
    document.forms["signup"].elements["account4"].disabled=false;
    document.forms["signup"].elements["account5"].disabled=false;

    else {document.forms["signup"].elements["account1"].disabled=true;
    document.forms["signup"].elements["account2"].disabled=true;
    document.forms["signup"].elements["account3"].disabled=true;
    document.forms["signup"].elements["account4"].disabled=true;
    document.forms["signup"].elements["account5"].disabled=true;
    }


    Thanks again,
    Kim


    ps. Can someone tell me what the best way to learn javascript is. Should I be reading a book???

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Can you give us a demo/HTML source of your page?

    Should I be reading a book???
    No. You can find all the information you need, from the internet. If you can't understand it/too complex it's ok to start of another thread here.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Please show us your live page, preferably using my code.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    preferably using my code.
    Which, by the way, will save you lots of time. As I've said, it's way more convenient, and used more practically.
    - Mike

  10. #10
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I tried to use your code Twey, but I couldn't get it to work.

    Here is the page:

    http://www.spidersneeze.com/form/keyword.htm

    The five radio buttons at the top are supposed to enable the 5 text fields below them.

    The first radio button should enable text field 1
    The second radio button should enable both text field 1 and 2
    The third radio button should enable text fields 1,2, and 3
    The fourth radio button should enable text fields 1,2,3, and 4
    The fifth radio button should enable text fields 1,2,3,4, and 5.

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
  •