Using a Varibal on and object

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

    #1

    Using a Varibal on and object

    I have multiple text fields in my page called
    Con_Hours0 Old_Con_Hours0
    Con_Hours1 Old_Con_Hours1
    Con_Hours2 Old_Con_Hours2
    Con_Hours3 Old_Con_Hours3
    etc
    etc

    In my java script I want to loop through and pick up the values of each
    field so I was looking at replacing the number with a variable
    sort of like this


    Count = 0

    While Count < 10
    if DocumentName.Co n_Hours" + Count + ".value != DocumentName.Ol d_Con_Hours"
    + Count + ".value
    {
    alert("Things Have been Changed in Row Number" + Count);
    }
    loop

    I can not get it to work, can you guys advise where I am going wrong?


    Many Thanks

    Mark


  • RobB

    #2
    Re: Using a Varibal on and object

    sh0t2bts wrote:[color=blue]
    > I have multiple text fields in my page called
    > Con_Hours0 Old_Con_Hours0
    > Con_Hours1 Old_Con_Hours1
    > Con_Hours2 Old_Con_Hours2
    > Con_Hours3 Old_Con_Hours3
    > etc
    > etc
    >
    > In my java script I want to loop through and pick up the values of[/color]
    each[color=blue]
    > field so I was looking at replacing the number with a variable
    > sort of like this
    >
    >
    > Count = 0
    >
    > While Count < 10
    > if DocumentName.Co n_Hours" + Count + ".value !=[/color]
    DocumentName.Ol d_Con_Hours"[color=blue]
    > + Count + ".value
    > {
    > alert("Things Have been Changed in Row Number" + Count);
    > }
    > loop
    >
    > I can not get it to work, can you guys advise where I am going wrong?
    >
    >
    > Many Thanks
    >
    > Mark[/color]

    Interesting syntax. Interesting, just not JavaScript.

    var n = 0,
    Con_Hours,
    Old_Con_Hours,
    f;
    if (f = document.forms[0])
    {
    var els = f.elements;
    while ((Con_Hours = els['Con_Hours' + n])
    && (Old_Con_Hours = els['Old_Con_Hours' + n]))
    {
    if (Con_Hours.valu e != Old_Con_Hours.v alue)
    alert("Things Have been Changed in Row Number " + n);
    ++n;
    }
    }

    Read this:



    Comment

    • Robert

      #3
      Re: Using a Varibal on and object

      In article <t06Pd.1675$cX3 .11@newsfe1-win.ntli.net>,
      "sh0t2bts" <sh0t2bts@hotma il.com> wrote:

      [color=blue]
      > In my java script I want to loop through and pick up the values of each
      > field so I was looking at replacing the number with a variable
      > sort of like this
      >
      >
      > Count = 0
      >
      > While Count < 10
      > if DocumentName.Co n_Hours" + Count + ".value != DocumentName.Ol d_Con_Hours"
      > + Count + ".value[/color]

      I do not recognize this as valid javascript syntax.

      I suggest that you invest in a Javascript book. The best book for
      experienced programmers is: Javascript: The Definitive Guide by David
      Flanagan.

      Here is a possible solution:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <title>Saving stats</title>
      <base href="http://spaceplace.jpl. nasa.gov/en/">
      <script type="text/javascript">
      /*
      Con_Hours0 Old_Con_Hours0
      Con_Hours1 Old_Con_Hours1
      Con_Hours2 Old_Con_Hours2
      Con_Hours3 Old_Con_Hours3
      etc
      etc

      In my java script I want to loop through and pick up the values of each
      field so I was looking at replacing the number with a variable
      sort of like this

      */




      // array is a good place to store a numeric list

      var old_con_hours = [ ]

      function validate(x)
      {

      // When true, we found a change so return false which means the form
      will
      // be submitted.
      // When false, we found a change so return false which means the form
      will
      // not be submitted.
      var proceed = true;

      for (var i = 0;;i++)
      {

      // Check if the text field exists.
      if (x.elements["Con_hours" +i])
      {
      if (x.elements["Con_hours" +i].value != old_con_hours[i] )
      {
      old_con_hours[i] = x.elements["Con_hours" +i].value;
      alert("Con_hour s number " + i + " has changed.");
      proceed = false;
      }
      }
      else
      {
      // since the text fields doesn't exist, we have reached the end
      break;
      }

      }

      return proceed;

      }

      </script>
      </head>
      <body >

      <P>Sum up a bunch of text fields.</p>

      <form name="myForm"
      action="http://www.natAValidWe bAddress.com"
      method="POST"
      onsubmit="retur n validate(docume nt.forms['myForm']);">

      <p>Count Hours:
      <br><br>
      0: <input type="text" name="Con_hours 0" size="40">
      <br>
      1: <input type="text" name="Con_hours 1" size="40">
      <br>
      2: <input type="text" name="Con_hours 2" size="40"></p>
      <br>
      <br>
      <input type="submit" value="Submit address information">
      </form>
      </p>
      </body>
      </html>

      Comment

      Working...