SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Macchiato's Avatar
    Join Date
    Nov 2009
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Disable Certain Input Boxes With Javascript

    I'd like to disable certain input boxes, but without changing the HTML code manually.

    For example:
    I'd like to disable the input boxes with the following id's:
    id="first_field"
    id="fourth_field"
    id="sixth_field"


    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Disable Certain Input Boxes With Javascript</title>
    </head>
    
    <body>
    
      <div>
        <form action="">
          <fieldset>
            <input type="text" id="first_field"/>
            <input type="text" id="second_field"/>
            <input type="text" id="third_field"/>
            <input type="text" id="fourth_field"/>
            <input type="text" id="fifth_field"/>
            <input type="text" id="sixth_field"/>
          </fieldset>
        </form>
      </div>
    
    </body>
    </html>

    Anyone know a Javascript that can do this?
    "Take a seat and have a coffee." -- Macchiato

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Hi,

    add this to the bottom of your HTML document, just before the closing <body> tag.

    Code JavaScript:
    <script type="text/javascript">
      var els = ["first_field", "fourth_field", "sixth_field"]
      for (var i = 0; i < els.length; i++)
      document.getElementById(els[i]).disabled = true;
    </script>

    This should do what you want.

  3. #3
    SitePoint Zealot Macchiato's Avatar
    Join Date
    Nov 2009
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, much appreciated!
    "Take a seat and have a coffee." -- Macchiato


Tags for this Thread

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
  •