SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Non-Member
    Join Date
    Nov 2005
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disable Text Fields?

    I'm what you would call a javascript noob. I'm familiar with web languages though so I should unserstand the code relatively easy.

    What I want is this. When the user clicks a certain radio button, I want three text fields to become disabled.

    Here's a dumbed down version of what I have...

    HTML Code:
    <label><input name="s_type" type="radio" value="ftp"  />FTP</label>
    <label><input name="s_type" type="radio" value="http" />HTTP</label><br><br>
    Server:
    <input name="ftp_server" type="text" id="ftp_server" size="35" /><br>
    Username:
    <input name="ftp_username" type="text" id="ftp_username" size="35" /><br>
    Password:
    <input name="ftp_password" type="password" id="ftp_password" size="35" />
    Could anyone show me how to disable all the text fields above when the user clicks the "http" radio button?

    Cheers!

  2. #2
    SitePoint Zealot yacka's Avatar
    Join Date
    Aug 2006
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need a function to get the textfields and set their disabled state appropriately, then you need to add an onclick event handler to each radio button. The http one to disable the textfields and the ftp one to re-enable them if they're disabled.

    Like the following:

    function settextfields(state)
    {
    var ftp_server = document.getElementById("ftp_server");
    var ftp_user = document.getElementById("ftp_username");
    var ftp_pass = document.getElementById("ftp_password");

    ftp_server.disabled = ! state;
    ftp_user.disabled = ! state;
    ftp_pass.disabled = ! state;

    }


    <label><input name="s_type" type="radio" value="ftp" onclick="settextfields(true)"/>FTP</label>
    <label><input name="s_type" type="radio" value="http" onclick="settextfields(false)"/>HTTP</label><br><br>

    It might be a good idea to add some visual cue so the user knows they're disabled.

  3. #3
    Non-Member
    Join Date
    Nov 2005
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks yacka, that's exactly what I needed


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
  •