Shortnening my JS DOM references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Astra

    Shortnening my JS DOM references

    Hi All

    In one of my JS files, I have a lot of what appears to be unnecessary and
    repetitive text, eg:

    document.order. cardnumberbox1. value = '';
    document.order. cardnumberbox2. value = '';
    document.order. cardnumberbox3. value = '';
    document.order. cardnumberbox4. value = '';
    document.order. cardnumberbox5. value = '';
    document.order. cardholder.valu e = '';
    document.order. expirymonth.val ue = '01';
    document.order. expiryyear.valu e = '2003';
    document.order. startmonth.valu e = 'N/A';
    document.order. startyear.value = 'N/A';
    document.order. issuenumber.val ue = 'N/A';
    document.order. securitycode.va lue = '';

    To try to shorten the lines and my overall text size I thought I would do
    the following:

    var doc = 'document.order ';

    doc.cardnumberb ox1.value = '';
    doc.cardnumberb ox2.value = '';
    doc.cardnumberb ox3.value = '';
    etc... etc..

    But it doesn't work.

    If poss, could you please advise me as to where I'm gonig wrong.

    Thanks Robbie


  • Joakim Braun

    #2
    Re: Shortnening my JS DOM references

    "Astra" <info@NoEmail.c om> skrev i meddelandet
    news:10quf32bre k124f@corp.supe rnews.com...[color=blue]
    > Hi All
    >
    > In one of my JS files, I have a lot of what appears to be unnecessary and
    > repetitive text, eg:
    >
    > document.order. cardnumberbox1. value = '';
    > document.order. cardnumberbox2. value = '';
    > document.order. cardnumberbox3. value = '';
    > document.order. cardnumberbox4. value = '';
    > document.order. cardnumberbox5. value = '';
    > document.order. cardholder.valu e = '';
    > document.order. expirymonth.val ue = '01';
    > document.order. expiryyear.valu e = '2003';
    > document.order. startmonth.valu e = 'N/A';
    > document.order. startyear.value = 'N/A';
    > document.order. issuenumber.val ue = 'N/A';
    > document.order. securitycode.va lue = '';
    >
    > To try to shorten the lines and my overall text size I thought I would do
    > the following:
    >
    > var doc = 'document.order ';[/color]
    <snip>

    var doc=document.or der;

    You want a reference to a form, not a concatenated string.

    Joakim Braun


    Comment

    • Tom Szabo

      #3
      Re: Shortnening my JS DOM references

      [color=blue]
      > var doc = 'document.order '; >> > var doc = document.order;[/color]

      without quotes, it should work

      Regards,

      Tom


      Comment

      • Michael Winter

        #4
        Re: Shortnening my JS DOM references

        On Thu, 2 Dec 2004 16:01:03 -0000, Astra <info@NoEmail.c om> wrote:

        [snip]
        [color=blue]
        > var doc = 'document.order ';
        >
        > doc.cardnumberb ox1.value = '';[/color]

        As others have pointed out, the code above creates a string, doc, not a
        reference to the form. Instead, use:

        var form = document.forms['order'],
        elem = form.elements;

        /* which could be shortened to
        * var elem = document.forms['order'].elements;
        */

        elem['cardnumberbox1 '].value = '';

        The forms and elements collections are the most cross-browser approach to
        referencing forms and their controls.

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Astra

          #5
          Re: Shortnening my JS DOM references

          Thanks Guys

          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opsid2a4x5 x13kvk@atlantis ...
          On Thu, 2 Dec 2004 16:01:03 -0000, Astra <info@NoEmail.c om> wrote:

          [snip]
          [color=blue]
          > var doc = 'document.order ';
          >
          > doc.cardnumberb ox1.value = '';[/color]

          As others have pointed out, the code above creates a string, doc, not a
          reference to the form. Instead, use:

          var form = document.forms['order'],
          elem = form.elements;

          /* which could be shortened to
          * var elem = document.forms['order'].elements;
          */

          elem['cardnumberbox1 '].value = '';

          The forms and elements collections are the most cross-browser approach to
          referencing forms and their controls.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.


          Comment

          Working...