what's wrong with this line?

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

    #1

    what's wrong with this line?

    Hi

    I used this for loop in my code, but I keep getting a syntax error on this
    line:

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


    Here's the rest of the function:

    function checkForm(oForm ){
    var totalRecords = document.getEle mentById("cbsTo tal");
    variation = eval('oForm.' + 'variation' + 0 + '.value');
    changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
    changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 + '.value');
    tr = totalRecords.va lue;
    for(var i=0;i<tr;i++){
    alert(variation );
    if(variation <> 0){
    if(changeNumber = ""){
    if(changeDescri ption = ""){
    alert("You have not entered a change number for variation" + i);
    return false;
    }
    }
    }

    }


  • Erwin Moller

    #2
    Re: what's wrong with this line?

    Marco Alting wrote:
    [color=blue]
    > Hi
    >
    > I used this for loop in my code, but I keep getting a syntax error on this
    > line:
    >
    > for(var i=0;i<tr;i++){
    >
    >
    > Here's the rest of the function:
    >
    > function checkForm(oForm ){
    > var totalRecords = document.getEle mentById("cbsTo tal");
    > variation = eval('oForm.' + 'variation' + 0 + '.value');
    > changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
    > changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 + '.value');
    > tr = totalRecords.va lue;[/color]

    add this line:

    alert ("tr="+tr+" and variation ="+variation +" and
    changeNumber="+ changeNumber+" and changeDescripti on="+changeDesc ription);

    Do you get what you expected?
    [color=blue]
    > for(var i=0;i<tr;i++){
    > alert(variation );
    > if(variation <> 0){
    > if(changeNumber = ""){
    > if(changeDescri ption = ""){
    > alert("You have not entered a change number for variation" + i);
    > return false;
    > }
    > }
    > }
    >
    > }[/color]

    Comment

    • Janwillem Borleffs

      #3
      Re: what's wrong with this line?


      "Marco Alting" <marco@alting-multimedia.nl> schreef in bericht
      news:rfh8b.1455 6$%47.1483749@a msnews03.chello .com...[color=blue]
      > Hi
      >
      > I used this for loop in my code, but I keep getting a syntax error on this
      > line:
      >
      > for(var i=0;i<tr;i++){
      >
      >
      > Here's the rest of the function:
      >
      > function checkForm(oForm ){
      > var totalRecords = document.getEle mentById("cbsTo tal");[/color]

      That's probably because the 'cbsTotal' object doesn't have a value property,
      so tr remains undefined.

      BTW, don't use eval when you don't need to.

      You can replace the lines:
      variation = eval('oForm.' + 'variation' + 0 + '.value');
      changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
      changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 + '.value');

      With:
      variation = oForm.elements['variation0'].value;
      changeNumber = oForm.elements['changeNumber0'].value;
      changeDescripti on = oForm.elements['changeDescript ion0'].value;


      JW



      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: what's wrong with this line?

        "Marco Alting" <marco@alting-multimedia.nl> writes:
        [color=blue]
        > I used this for loop in my code, but I keep getting a syntax error on this
        > line:
        >
        > for(var i=0;i<tr;i++){[/color]

        The error is probably not on that line, but in that statement.
        It is in the contents of the for statment:
        [color=blue]
        > if(variation <> 0){[/color]

        In Javascript, the not-equal operator is "!=", not "<>" (as in BASICs).
        This is a syntax error.
        [color=blue]
        > if(changeNumber = ""){[/color]

        Also, the comparison operator is "==", not "=". This is an assignment,
        which changes "changeNumb er" to the empty string. The value of an
        assignment is the value that is assigned, in this case the empty string.
        In an if condition, it is automatically converted to a boolean, and
        the empty string converts to false.
        [color=blue]
        > if(changeDescri ption = ""){[/color]

        ditto.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: what's wrong with this line?

          "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> writes:[color=blue]
          >
          > You can replace the lines:[/color]
          [using eval][color=blue]
          > With:
          > variation = oForm.elements['variation0'].value;
          > changeNumber = oForm.elements['changeNumber0'].value;
          > changeDescripti on = oForm.elements['changeDescript ion0'].value;[/color]

          A very good advise. I would even change "oFrom" to
          "document.f orms['oForm']". That should work in all browser, current
          and future (if they implement the W3C DOM).

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Marco Alting

            #6
            Re: what's wrong with this line?

            Thanks for all the advises, I think the error was in the if statements.
            Here's the working code:

            function checkForm(oForm ){
            var totalRecords = document.getEle mentById("cbsTo tal");
            tr = totalRecords.va lue;
            for(var i=0;i<tr;i++){
            alert(eval('oFo rm.' + 'variation' + i + '.value'));
            if(eval('oForm. ' + 'variation' + i + '.value') != '0'){
            if(eval('oForm. ' + 'changeNumber' + i + '.value') == '0'){
            if(eval('oForm. ' + 'changeDescript ion' + 0 + '.value') == '0'){
            alert("You have not entered a change number for variation" + i);
            return false;
            }
            }
            }
            }
            }


            "Janwillem Borleffs" <jwb@jwbfoto.de mon.nl> schreef in bericht
            news:3f61a532$0 $28910$1b62eedf @news.euronet.n l...[color=blue]
            >
            > "Marco Alting" <marco@alting-multimedia.nl> schreef in bericht
            > news:rfh8b.1455 6$%47.1483749@a msnews03.chello .com...[color=green]
            > > Hi
            > >
            > > I used this for loop in my code, but I keep getting a syntax error on[/color][/color]
            this[color=blue][color=green]
            > > line:
            > >
            > > for(var i=0;i<tr;i++){
            > >
            > >
            > > Here's the rest of the function:
            > >
            > > function checkForm(oForm ){
            > > var totalRecords = document.getEle mentById("cbsTo tal");[/color]
            >
            > That's probably because the 'cbsTotal' object doesn't have a value[/color]
            property,[color=blue]
            > so tr remains undefined.
            >
            > BTW, don't use eval when you don't need to.
            >
            > You can replace the lines:
            > variation = eval('oForm.' + 'variation' + 0 + '.value');
            > changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
            > changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 +[/color]
            '.value');[color=blue]
            >
            > With:
            > variation = oForm.elements['variation0'].value;
            > changeNumber = oForm.elements['changeNumber0'].value;
            > changeDescripti on = oForm.elements['changeDescript ion0'].value;
            >
            >
            > JW
            >
            >
            >[/color]


            Comment

            • Marco Alting

              #7
              Re: what's wrong with this line?

              When I comment out the for loop and add the alert, I get what I
              expected, but the for loop still gives an error



              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Geir Klemetsen

                #8
                Re: what's wrong with this line?


                "Marco Alting" <marco@alting-multimedia.nl> skrev i melding
                news:rfh8b.1455 6$%47.1483749@a msnews03.chello .com...[color=blue]
                > Hi
                >
                > I used this for loop in my code, but I keep getting a syntax error on this
                > line:
                >
                > for(var i=0;i<tr;i++){[/color]

                What's "tr". Has it a valid number?
                [color=blue]
                > Here's the rest of the function:
                >
                > function checkForm(oForm ){
                > var totalRecords = document.getEle mentById("cbsTo tal");
                > variation = eval('oForm.' + 'variation' + 0 + '.value');
                > changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
                > changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 + '.value');
                > tr = totalRecords.va lue;
                > for(var i=0;i<tr;i++){
                > alert(variation );
                > if(variation <> 0){
                > if(changeNumber = ""){
                > if(changeDescri ption = ""){
                > alert("You have not entered a change number for variation" + i);
                > return false;
                > }
                > }
                > }
                >
                > }
                >
                >[/color]


                Comment

                • Douglas Crockford

                  #9
                  Re: what's wrong with this line?

                  > function checkForm(oForm ){[color=blue]
                  > var totalRecords = document.getEle mentById("cbsTo tal");
                  > variation = eval('oForm.' + 'variation' + 0 + '.value');
                  > changeNumber = eval('oForm.' + 'changeNumber' + 0 + '.value');
                  > changeDescripti on = eval('oForm.' + 'changeDescript ion' + 0 + '.value');
                  > tr = totalRecords.va lue;
                  > for(var i=0;i<tr;i++){
                  > alert(variation );
                  > if(variation <> 0){
                  > if(changeNumber = ""){
                  > if(changeDescri ption = ""){
                  > alert("You have not entered a change number for variation" + i);
                  > return false;
                  > }
                  > }
                  > }
                  > }[/color]

                  I ran it through jslint and found these problems:

                  '<>' should be '!=='. This isn't Visual Basic.

                  When making an equality comparison, use '===', not '='

                  Also, you are making extremely bad use of the eval function. Never is eval. The
                  expression

                  eval('oForm.' + 'variation' + 0 + '.value')

                  probably wants to be

                  oForm['variation' + 0].value

                  If there is a chance that totalRecords is not an object, then wrap the for in
                  an if :

                  if (totalRecords) {
                  for(var i=0; i < tr; i++) {




                  Comment

                  Working...