Value of string???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ckerns@fusert.net

    #1

    Value of string???

    I have a form with 40 text boxes (default of each = 0). I want to
    evaluate each box to see if the value is still zero, and if it is, I
    want to add it to a "Zero Count". The box names are "L411 thru L419",
    "L421 thru L429", "L431 thru L439" and "L441 thru L449".

    Example:

    L41 - 002000500
    L42 - 000030000
    L43 - 000000000
    L44 - 000000000

    I have construct the control name thru a couple of nested for loops
    and all is well (constructed string = "MyForm.L411.va lue").

    When I plug this into an If statement to get the value of the control,
    everything dies. Here is my If:

    for(var j = 1; j < 5; j++)
    {
    rowString = "L4";

    // builds L41
    rowString = rowString + j;

    for(var x = 1; x < 10; x++)
    {
    // builds L411
    rowString = rowString + x;

    // Builds MyForm.L411.val ue
    rowString = "MyForm." + rowString + ".value"

    //alert shows "MyForm.L411.va lue" - without quotes
    //alert(rowString );

    if( rowString = "0")
    {
    zeroCount++:

    }
    rowString = "L4" + j;
    }
    }

    How can I test the value of a control, based on a string built?

    Many tia's

    ck
  • RobG

    #2
    Re: Value of string???

    ckerns@fusert.n et wrote:[color=blue]
    > I have a form with 40 text boxes (default of each = 0). I want to
    > evaluate each box to see if the value is still zero, and if it is, I
    > want to add it to a "Zero Count". The box names are "L411 thru L419",
    > "L421 thru L429", "L431 thru L439" and "L441 thru L449".
    >
    > Example:
    >
    > L41 - 002000500
    > L42 - 000030000
    > L43 - 000000000
    > L44 - 000000000
    >
    > I have construct the control name thru a couple of nested for loops
    > and all is well (constructed string = "MyForm.L411.va lue").
    >
    > When I plug this into an If statement to get the value of the control,
    > everything dies. Here is my If:
    >
    > for(var j = 1; j < 5; j++)
    > {
    > rowString = "L4";
    >
    > // builds L41
    > rowString = rowString + j;
    >
    > for(var x = 1; x < 10; x++)
    > {
    > // builds L411
    > rowString = rowString + x;
    >
    > // Builds MyForm.L411.val ue
    > rowString = "MyForm." + rowString + ".value"[/color]

    Ooops... you have made 'rowString' the literal string
    "MyForm.L411.va lue" which is not what you want to do.

    What you want is:

    rowString = MyForm.rowStrin g.value;

    presuming that the form has an element with a name equal to the value
    of 'rowString', i.e. L411.

    But this is dangerous, test that the element exists before trying to
    get its value, 'cos if it doesn't, your script will die:

    if ( MyForm.rowStrin g ) {
    rowString = Myform.rowStrin g.value;
    } else {
    rowString = undefined;
    }

    which can be abbreviated to:

    rowString = ( MyForm.rowStrin g && MyForm.rowStrin g.value );

    I would suggest changing the name to rowValue, but that's just me.
    Reusing variables this way may make maintenance more difficult, but
    again, that's up to you. It may be better to build up the element
    name in a variable called 'eleName', then assign the value to
    'eleValue' so things are a bit clearer and you'd get:

    eleValue = ( MyForm.eleName && MyForm.eleName. value );
    [color=blue]
    >
    > //alert shows "MyForm.L411.va lue" - without quotes
    > //alert(rowString );
    >
    > if( rowString = "0")[/color]

    Instead of testing whether the value of 'rowString' is equivalent to
    the string '0', you are assigning it the value of the string '0'.

    As a hint always write such tests:

    if( '0' = rowString )

    That way you will get a script error rather than just erroneous
    results. The correct syntax (assuming you want to test for equality)
    is:

    if( '0' == rowString )
    [color=blue]
    > {
    > zeroCount++:
    >
    > }
    > rowString = "L4" + j;
    > }
    > }
    >
    > How can I test the value of a control, based on a string built?
    >
    > Many tia's
    >
    > ck[/color]


    --
    Rob

    Comment

    • Dietmar Meier

      #3
      Re: Value of string???

      ckerns@fusert.n et wrote:
      [color=blue]
      > I have a form with 40 text boxes (default of each = 0). I want to
      > evaluate each box to see if the value is still zero, and if it is, I
      > want to add it to a "Zero Count". The box names are "L411 thru L419",
      > "L421 thru L429", "L431 thru L439" and "L441 thru L449".[/color]

      (That are 36, not 40, text inputs).

      var i, j, nZeroCount, oForm, oCurElem, oCurVal;
      oForm = document.forms['MyForm'];
      nZeroCount = 0;
      for (i=1; i<10; i++) {
      for (j=1; j<5; j++) {
      oCurElem = oForm.elements["L4" + j + i];
      if (oCurElem) {
      oCurVal = oCurElem.value;
      if (!isNaN(oCurVal ) && parseInt(oCurVa l, 10) == 0) {
      nZeroCount++;
      }
      }
      }
      }

      ciao, dhgm

      Comment

      • Douglas Crockford

        #4
        Re: Value of string???

        > When I plug this into an If statement to get the value of the control,[color=blue]
        > everything dies. Here is my If:[/color]
        [color=blue]
        > if( rowString = "0")[/color]

        If you had used JSLint, it would have alerted you to this statement. You
        probably meant

        if (rowString == "0") {

        See http://www.JSLint.com

        Comment

        • ckerns@refuse.net

          #5
          Re: Value of string???

          Thank you very much! That got me on the right track now! :)

          Although, if I use basically the same loop earlier in the structure,
          how can I assign a "0" to non-numeric entries? Granted what I'm
          checking for (below) is null, but I don't think that is exactly what
          I want to do. I believe I should be checking for "non-numeric"
          instead.

          This is what I am trying, but it doesn't work :(

          for(var j = 1; j < 5; j++)
          {
          rowString = "L4";
          rowString = rowString + j;

          for(var x = 1; x < 10; x++)
          {
          rowString = rowString + x;


          if (eval ("document.form s[0]." + rowString + ".value") ==
          Null)
          {
          MyForm.rowstrin g.value = 0;
          }
          rowString = "L4" + j;
          }
          }

          ck

          Comment

          • RobG

            #6
            Re: Value of string???

            ckerns@refuse.n et wrote:[color=blue]
            > Thank you very much! That got me on the right track now! :)
            >
            > Although, if I use basically the same loop earlier in the structure,
            > how can I assign a "0" to non-numeric entries? Granted what I'm
            > checking for (below) is null, but I don't think that is exactly what
            > I want to do. I believe I should be checking for "non-numeric"
            > instead.
            >
            > This is what I am trying, but it doesn't work :(
            >
            > for(var j = 1; j < 5; j++)
            > {
            > rowString = "L4";
            > rowString = rowString + j;[/color]

            Save yourself some typing, two lines to one:

            rowString = "L4" + j;
            [color=blue]
            >
            > for(var x = 1; x < 10; x++)
            > {
            > rowString = rowString + x;[/color]

            or

            rowString += x;

            Since rowString is a string, x will be concatenated (i.e. appended)
            to the string.
            [color=blue]
            >
            >
            > if (eval ("document.form s[0]." + rowString + ".value") ==
            > Null)[/color]

            Ditch 'eval', it is nearly never, ever needed.

            The value of an empty input is '' (empty string), not 'null' (note
            captialisation) .

            I'll assume that what you want to do is see if the value is zero. If
            so, add it to the zero count. If it's only digits, leave it alone.
            If it's anything else, give an error. So here goes:

            // Mentioned sensible variable names before
            var eleValue;

            // Make sure the element exists, then get its value
            if ( document.forms[0].rowString
            && ( eleValue = document.forms[0].rowString.valu e )) {

            // If the value is zero, add one to zeroCount
            if ( eleValue == 0 ){
            zeroCount++;

            // If there is anything in the value that isn't 0-9
            } else if ( /\D/g.test(eleValue ) ) {
            // Do whatever needs to be done if there are
            // things in the value that aren't digits
            }
            }


            [...]

            --
            Rob

            Comment

            • Dr John Stockton

              #7
              Re: Value of string???

              JRS: In article <42625d97$0$923 0$5a62ac22@per-qv1-newsreader-
              01.iinet.net.au >, dated Sun, 17 Apr 2005 22:56:17, seen in
              news:comp.lang. javascript, RobG <rgqld@iinet.ne t.auau> posted :[color=blue]
              >
              > Ditch 'eval', it is nearly never, ever needed.
              >[/color]

              Yes and no. One rarely needs to write it; OTOH, it and function call
              are probably the two statements that I execute most frequently.

              <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>; View Source, or enter
              remoteEval.toSt ring()
              in the textarea and press RmEv !




              BTW, could someone(s) look at
              <URL:http://www.merlyn.demo n.co.uk/js-boxes.htm>
              with non-MSIE to see if all is reasonably well?

              The main thing is the box starting -- New Code : in which ShoTrim
              will I hope be shown as containing the string "' comment ? '" and
              ShoLinCnt's first line should be the only lone that wraps and a line
              -- One visible blank line next; scroll for more code :
              should be true. And function NotALot should fit the next box.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • RobG

                #8
                Re: Value of string???

                Dr John Stockton wrote:[color=blue]
                > JRS: In article <42625d97$0$923 0$5a62ac22@per-qv1-newsreader-
                > 01.iinet.net.au >, dated Sun, 17 Apr 2005 22:56:17, seen in
                > news:comp.lang. javascript, RobG <rgqld@iinet.ne t.auau> posted :
                >[color=green]
                >> Ditch 'eval', it is nearly never, ever needed.
                >>[/color]
                >
                >
                > Yes and no. One rarely needs to write it; OTOH, it and function call
                > are probably the two statements that I execute most frequently.
                >
                > <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>; View Source, or enter
                > remoteEval.toSt ring()
                > in the textarea and press RmEv !
                >
                >
                >
                >
                > BTW, could someone(s) look at
                > <URL:http://www.merlyn.demo n.co.uk/js-boxes.htm>
                > with non-MSIE to see if all is reasonably well?[/color]

                Gosh, nearly missed you way down here!
                [color=blue]
                >
                > The main thing is the box starting -- New Code : in which ShoTrim
                > will I hope be shown as containing the string "' comment ? '" and[/color]

                Nope, does not appear. Comparison with IE shows it to be totally
                missing in Firefox.
                [color=blue]
                > ShoLinCnt's first line should be the only lone that wraps and a line[/color]

                The comment that causes the line to wrap in IE is not there in
                Firefox.
                [color=blue]
                > -- One visible blank line next; scroll for more code :
                > should be true.[/color]

                Yes, but the IE code is missing a few linefeeds after semi-colons
                that are in Firefox and comments vanish:

                FF:

                function ShoDoo(Fn) {
                var St = ShoTrim(Fn.toSt ring());
                Depict(BoxX, ShoLinCnt(St, BoxX), St, "red");
                Fn();
                return "";


                IE:

                function ShoDoo(Fn) { // N.B. this calls Fn() - ADD show others ?
                var St = ShoTrim(Fn.toSt ring())
                Depict(BoxX, ShoLinCnt(St, BoxX), St, "red") ; Fn() ; return "" }

                [color=blue]
                > And function NotALot should fit the next box.
                >[/color]

                FF:

                function NotALot() {
                }

                Yup, that's not a lot!! The comments show up in Opera though.

                :-)

                In general, Opera does it all differently again - dropping some
                comments, adding in new lines and dropping others...


                --
                Rob

                Comment

                • Brian Munroe

                  #9
                  Re: Value of string???

                  RobG wrote:
                  [color=blue]
                  > Ditch 'eval', it is nearly never, ever needed.
                  >[/color]
                  [snip]

                  Hi, I don't mean to hijack the thread to start beating a dead horse,
                  but I can't get your eval replacement code to work. I think I
                  implemented it correctly in the sayHi() function. eleValue is always
                  returned undefined, any suggestions?

                  thanks

                  -- brian

                  <html>
                  <head>
                  <script type="text/javascript">

                  function sayHi() {
                  var eleValue;
                  var rowString = "txt1";
                  if ( document.forms[0].rowString && ( eleValue =
                  document.forms[0].rowString.valu e )) {
                  alert("hello");
                  } else {
                  alert(eleValue) ;
                  }
                  }

                  function sayHi2() {
                  var rowString = "txt1";
                  res = eval("document. forms[0]." + rowString + ".value");
                  alert(res);
                  }

                  </script>
                  </head>
                  <body>

                  <form name="main">
                  <input type="text" name="txt1" size="10" value="hi" />
                  <input type="button" onclick="sayHi( );" />
                  </form>
                  </body>
                  </html>

                  Comment

                  • Dietmar Meier

                    #10
                    Re: Value of string???

                    Brian Munroe wrote:
                    [color=blue]
                    > var rowString = "txt1";
                    > if ( document.forms[0].rowString && ( eleValue =
                    > document.forms[0].rowString.valu e )) {
                    > alert("hello");
                    > } else {
                    > alert(eleValue) ;
                    > }
                    > }[/color]

                    var rowString = "txt1";
                    if (document.forms[0][rowString]
                    && (eleValue = document.forms[0][rowString].value)
                    ) {
                    alert("hello");
                    }
                    else {
                    alert(eleValue) ;
                    }

                    ciao, dhgm

                    Comment

                    • Brian Munroe

                      #11
                      Re: Value of string???

                      Dietmar Meier wrote:
                      [color=blue]
                      > if (document.forms[0][rowString]
                      > && (eleValue = document.forms[0][rowString].value)
                      >
                      > ciao, dhgm[/color]

                      Well that was easy enough!

                      thanks

                      -- brian

                      Comment

                      • Dr John Stockton

                        #12
                        Re: Value of string???

                        JRS: In article <W0%8e.988$Zn.4 8597@news.optus .net.au>, dated Tue, 19
                        Apr 2005 03:36:22, seen in news:comp.lang. javascript, RobG
                        <rgqld@iinet.ne t.auau> posted :[color=blue]
                        >Dr John Stockton wrote:[/color]
                        [color=blue][color=green]
                        >> BTW, could someone(s) look at
                        >> <URL:http://www.merlyn.demo n.co.uk/js-boxes.htm>
                        >> with non-MSIE to see if all is reasonably well?
                        >>
                        >> The main thing is the box starting -- New Code : in which ShoTrim
                        >> will I hope be shown as containing the string "' comment ? '" and[/color]
                        >
                        > Nope, does not appear. Comparison with IE shows it to be totally
                        > missing in Firefox.[/color]

                        Disappointing but not surprising.
                        An unused value is optimised out in Firefox.

                        After
                        function f() { x = "'comment'" ; var y = "'remark'" ; return 5 }
                        does f.toString() show the 'comment' and 'remark' in Firefox?
                        Delphi, at least, would optimise away those statements.

                        [color=blue][color=green]
                        >> -- One visible blank line next; scroll for more code :
                        >> should be true.[/color]
                        >
                        > Yes, but the IE code is missing a few linefeeds after semi-colons
                        > that are in Firefox[/color]

                        NO !! Firefox has inserted two linefeeds and a semi-colon ! My wish,
                        after all, is that the actual source be shown; and IE does that almost
                        perfectly (long white-space can, in IE4, cause premature wrapping).

                        [color=blue]
                        > In general, Opera does it all differently again - dropping some
                        > comments, adding in new lines and dropping others...[/color]


                        Still, you've seen nothing too bad. Thanks.

                        ISTM that I need a test case with a single statement that exceeds 68 or
                        69 characters, though I hope never to need to write one. One is now
                        added to function NotALot.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                        Comment

                        Working...