• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Protecting HTML Page Elements with JavaScript and CSS

    December 4, 2002 Timothy Prickett Morgan

    Hey, David:

    I have an HTML form with several optional elements. Is there a way to protect and unprotect text elements based on a checkbox? When the user checks an alternate shipping address box, I want to open up a set of shipping address fields. I tried to do this with JavaScript, but the value of the checkbox doesn’t seem to change in Internet Explorer.

    — Loren

    You were on the right track checking the value for the checkbox; however checkboxes are a special case.

    The value attribute for checkboxes represents what the browser sends to the server when the checkbox is checked but doesn’t indicate whether it is checked. There is a special checked property that indicates the checked state.

    I have provided a snippet of HTML code to get you started. In this example, I have also grayed out the shipping description fields to make it more obvious to the user that the fields are protected:

    <html>
    <head>
      <title&gt; Demonstrate Optional Form Elements</title>
      <style>
        .protected {
        color: gray;
        font-style: italic;}
        .unprotected {
        color: black;
        font-style: normal;}
      </style>
      <script language="JavaScript" type="text/javascript">
        function shipToChange() {
          if (document.getElementById("shipTo").checked == true) {
            document.getElementById("textShipTo").className="unprotected";
            document.getElementById("shipToName").disabled=false;
            document.getElementById("shipToName").focus();
            document.getElementById("shipToLine1").disabled=false;
            document.getElementById("shipToLine2").disabled=false;
            document.getElementById("shipToCity").disabled=false;
            document.getElementById("shipToState").disabled=false;
            document.getElementById("shipToZip").disabled=false;
          }
          else {
            document.getElementById("textShipTo").className="protected";
            document.getElementById("shipToName").disabled=true;
            document.getElementById("shipToLine1").disabled=true;
            document.getElementById("shipToLine2").disabled=true;
            document.getElementById("shipToCity").disabled=true;
            document.getElementById("shipToState").disabled=true;
            document.getElementById("shipToZip").disabled=true;
          }
        }
      </script>
    </head>
    <body>
      <form name="myForm" method="POST">
        <table>
          <tfoot>
            <tr>
              <td colspan="2">
                <input type="submit" name="submit" value="Update">
                <input type="submit" name="cancel" value="Cancel">
              </td>
            </tr>
          </tfoot>
          <tbody>
            <tr valign="top">
              <td>Billing Address:</td>
              <td>
                <input type="text" name="billToName" size="30"><br />
                <input type="text" name="billToLine1" size="30"><br />
                <input type="text" name="billToLine2" size="30"><br />
                <input type="text" name="billToCity" size="20">
                <input type="text" name="billToState" size="2">
                <input type="text" name="billToZip" size="10">
              </td>
            </tr>
            <tr align="top" valign="top">
              <td>Specify Shipping Address:</td>
              <td><input type="checkbox" name="shipTo" onchange="shipToChange();"></td>
            </tr>
        <tr valign="top">
              <td id="textShipTo">Ship To:</td>
              <td align="top">
                <input type="text" name="shipToName" size="30"><br />
                <input type="text" name="shipToLine1" size="30"><br />
                <input type="text" name="shipToLine2" size="30"><br />
                <input type="text" name="shipToCity" size="20">
                <input type="text" name="shipToState" size="2">
                <input type="text" name="shipToZip" size="10">
              </td>
            </tr>
          </tbody>
        </table>
      </form>
      <script language="JavaScript">
        shipToChange();
      </script>
    </html>
    

    To try this out on your computer, cut and paste the HTML source code listed above into an empty document and save it with a .html extension. When you are done, open it with your browser and try checking and unchecking the ship-to address. After toggling the checkbox, try typing an address in the ship-to address fields.

    The elements on this page that are referred to in the javascript all have a property name or ID set by the name attribute for <input> elements, and by the ID attribute of the <td> element. This choice let me use the document.getElementById() function to manipulate those elements attributes. When a change is detected in the shipTo element, the shipToChange() script is run to set the style attributes of the ship-to address elements.

    — David

    Sponsored By
    ADVANCED SYSTEMS CONCEPTS

    Business Analytics
    – Practical –
    – Cost Effective –
    – Easy to Deploy –

    SEQUEL FYI

    User Quote:
    “I love SEQUEL FYI because it lets me look at the data any way I need to see it, instantly. This is the easiest tool to manage complex product relationships that I have ever seen.”

    SEQUEL FYI offers outstanding OLAP business intelligence functionality for a fraction of the cost of comparable solutions.

    Read More > View Streaming Video

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: mgo_rc, Volume 2, Number 90 -- December 4, 2002

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Get Rid of Old IFS Files Reader Feedback and Insights: One-Row Table Still Needed

    Leave a Reply Cancel reply

MGO Volume: 2 Issue: 90

This Issue Sponsored By

    Table of Contents

    • Reader Feedback and Insights: RPG II
    • Protecting HTML Page Elements with JavaScript and CSS
    • LIKE is Like, Confusing, Man!

    Content archive

    • The Four Hundred
    • Four Hundred Stuff
    • Four Hundred Guru

    Recent Posts

    • IBM i Has a Future ‘If Kept Up To Date,’ IDC Says
    • When You Need Us, We Are Ready To Do Grunt Work
    • Generative AI: Coming to an ERP Near You
    • Four Hundred Monitor, March 22
    • IBM i PTF Guide, Volume 25, Number 12
    • Unattended IBM i Operations Continue Upward Climb
    • VS Code Is The Full Stack IDE For IBM i
    • Domino Runs on IBM i 7.5, But HCL Still Working on Power10
    • Four Hundred Monitor, March 6
    • IBM i PTF Guide, Volume 25, Number 11

    Subscribe

    To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

    Pages

    • About Us
    • Contact
    • Contributors
    • Four Hundred Monitor
    • IBM i PTF Guide
    • Media Kit
    • Subscribe

    Search

    Copyright © 2023 IT Jungle