How to compare 2 elements with same getElementById(x)?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vadymus@gmail.com

    How to compare 2 elements with same getElementById(x)?

    Hello,
    I need to compare 2 id names but not their values. For example:

    document.getEle mentById(i) == document.getEle mentById(j)

    these elements above seem to give error or do not work.
    My original statement is this (and note, when I add line above to it,
    that does not work):

    function uniqueValues(){
    var myForm = document.add2db ;
    var iMax = myForm.imax.val ue;
    //var IsSimilar, IsSame;
    for (i=0;i<iMax;i++ )
    {

    var fieldValue=docu ment.getElement ById(i).value;
    alert("id is "+document.getE lementById(i));
    for(j=0;j<iMax; j++)
    {
    var fieldValue2=doc ument.getElemen tById(j).value;
    if ((fieldValue == fieldValue2) && (document.getEl ementById(i) !=
    document.getEle mentById(j))) { //FIX HERE
    alert("I am sorry, but these values are identical:
    "+document.getE lementById(j).v alue);
    document.getEle mentById(j).foc us();
    return false;
    }
    }
    }
    //return true;
    }

  • bwucke@gmail.com

    #2
    Re: How to compare 2 elements with same getElementById( x)?

    > I need to compare 2 id names but not their values. For example:[color=blue]
    > document.getEle mentById(i) == document.getEle mentById(j)[/color]

    Well, there's this method, .isEqualNode() but since IDs are supposed to
    be unique, in your case i==j should perfectly suffice.

    Comment

    • McKirahan

      #3
      Re: How to compare 2 elements with same getElementById( x)?

      <vadymus@gmail. com> wrote in message
      news:1135288049 .567999.64720@o 13g2000cwo.goog legroups.com...[color=blue]
      > Hello,
      > I need to compare 2 id names but not their values. For example:
      >
      > document.getEle mentById(i) == document.getEle mentById(j)[/color]

      Try:

      document.getEle mentById(i).nam e == document.getEle mentById(j).nam e


      Comment

      • vadymus@gmail.com

        #4
        Re: How to compare 2 elements with same getElementById( x)?

        Thank you so much...
        I did finf .name but it did not work for me. Identical id's are not
        recognized (maybe I do smth wrong?):

        function uniqueValues(){
        var myForm = document.add2db ;
        var iMax = myForm.imax.val ue;
        for (i=0;i<iMax;i++ )
        {

        var fieldValue=docu ment.getElement ById(i).value;
        //alert("id is "+document.getE lementById(i).n ame);
        for(j=0;j<iMax; j++)
        {
        var fieldValue2=doc ument.getElemen tById(j).value;
        if ((fieldValue == fieldValue2) && (document.getEl ementById(i).na me
        != document.getEle mentById(j).nam e)) { //FIX HERE
        alert("I am sorry, but these values are identical:
        "+document.getE lementById(j).v alue);
        document.getEle mentById(j).foc us();
        return false;
        }
        }
        }
        //return true;
        }



        I am going to try .isEqualNode() now

        Comment

        • vadymus@gmail.com

          #5
          Re: How to compare 2 elements with same getElementById( x)?

          Lee, you are absolutely right i and j can be compared. I cannot believe
          it is more simple that I thought. However, my script does not catch
          identical values here still.

          I try to submit form with unique field values as below.
          - imax is the number of fields.
          - all unique field values are from textarea called texarea0, textarea1,
          .......texareaN (all together is imax)
          - these texarea fields have unique html IDs: 0, 1, 2, 3.....N

          Code below does not catch nonubique values eventhough there are no
          errors when run

          function uniqueValues(){
          var myForm = document.add2db ;
          var iMax = myForm.imax.val ue;
          for (i=0;i<iMax;i++ )
          {

          var fieldValue=docu ment.getElement ById(i).value;
          //alert("id is "+document.getE lementById(i).n ame);
          for(j=0;j<iMax; j++)
          {
          var fieldValue2=doc ument.getElemen tById(j).value;
          if ((fieldValue == fieldValue2) && (i!=j)) { //FIX
          HEREdocument.ge tElementById(i) .name != document.getEle mentById(j).nam e
          alert("I am sorry, but these values are identical:
          "+document.getE lementById(j).v alue);
          document.getEle mentById(j).foc us();
          return false;
          }
          }
          }
          //return true;
          }

          Comment

          • bwucke@gmail.com

            #6
            Re: How to compare 2 elements with same getElementById( x)?


            vadymus@gmail.c om napisal(a):[color=blue]
            > Lee, you are absolutely right i and j can be compared. I cannot believe
            > it is more simple that I thought. However, my script does not catch
            > identical values here still.
            >
            > I try to submit form with unique field values as below.
            > - imax is the number of fields.
            > - all unique field values are from textarea called texarea0, textarea1,
            > .......texareaN (all together is imax)
            > - these texarea fields have unique html IDs: 0, 1, 2, 3.....N
            >
            > Code below does not catch nonubique values eventhough there are no
            > errors when run
            >
            > function uniqueValues(){
            > var myForm = document.add2db ;
            > var iMax = myForm.imax.val ue;
            > for (i=0;i<iMax;i++ )
            > {
            >
            > var fieldValue=docu ment.getElement ById(i).value;
            > //alert("id is "+document.getE lementById(i).n ame);
            > for(j=0;j<iMax; j++)
            > {
            > var fieldValue2=doc ument.getElemen tById(j).value;
            > if ((fieldValue == fieldValue2) && (i!=j)) { //FIX
            > HEREdocument.ge tElementById(i) .name != document.getEle mentById(j).nam e
            > alert("I am sorry, but these values are identical:
            > "+document.getE lementById(j).v alue);
            > document.getEle mentById(j).foc us();
            > return false;
            > }
            > }
            > }
            > //return true;
            > }[/color]

            Not sure what's the problem now, but I see you're doing quite a few too
            many checks. That is, say, i=3, j=5, then j=3, i=5 will check the same.

            for (i=0; i < iMax-1 ; i++)...
            {
            ....
            for(j=i+1;j<iMa x;j++)
            {
            ....
            if (fieldValue == fieldValue2)
            {

            then i will never be equal to j and still all pairs get checked.

            Maybe send a piece of HTML, possible that you're mishandling IDs there
            somehow.

            Comment

            • RobG

              #7
              Re: How to compare 2 elements with same getElementById( x)?

              vadymus@gmail.c om wrote:[color=blue]
              > Lee, you are absolutely right i and j can be compared. I cannot believe
              > it is more simple that I thought. However, my script does not catch
              > identical values here still.
              >
              > I try to submit form with unique field values as below.
              > - imax is the number of fields.
              > - all unique field values are from textarea called texarea0, textarea1,
              > .......texareaN (all together is imax)
              > - these texarea fields have unique html IDs: 0, 1, 2, 3.....N[/color]

              This indicates that your HTML is invalid. IDs can include numbers,
              but can't start with them.

              [color=blue]
              >
              > Code below does not catch nonubique values eventhough there are no
              > errors when run[/color]

              The following loops through all the elements of the form and barfs
              when the first duplicate value is reached:

              function uniqueValues()
              {
              var val, vals = {};
              var el, els = document.forms['add2db'].elements;

              for (var i=0, len=els.length; i<len; ++i){
              el = els[i];
              val = el.value
              if (vals[val]){
              alert('Already have ' + val);
              if (el.focus) el.focus();
              return;
              }
              vals[val] = val;
              }
              }


              [...]


              --
              Rob

              Comment

              • bwucke@gmail.com

                #8
                Re: How to compare 2 elements with same getElementById( x)?

                > val = el.value[color=blue]
                > if (vals[val]){ }
                > vals[val] = val;[/color]

                except if the user enters two zeros.

                vals[val] = true;

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: How to compare 2 elements with same getElementById( x)?

                  bwucke@gmail.co m wrote:
                  [color=blue][color=green]
                  >> val = el.value
                  >> if (vals[val]){ }
                  >> vals[val] = val;[/color]
                  >
                  > except if the user enters two zeros.
                  >
                  > vals[val] = true;[/color]

                  What are you talking about? `vals' is a reference to an Object object.
                  Property names as well as form controls' values are _string_ values:

                  var vals = {"0": "0"}, val = "00";
                  if (vals[val])
                  {
                  // will never be executed
                  }

                  One problem that remains, however, is that Object objects inherit
                  properties from Object, so that test is not reliable; for retaining
                  and those inherited property values and avoiding false positives, it
                  is necessary to map form controls' values that correspond to those
                  properties' names to unused property names; one may instead also use
                  Object.prototyp e.hasOwnPropert y() where supported.

                  Please quote the minimum of what you are replying to and provide
                  attribution of quoted material:

                  <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
                  <URL:http://www.safalra.com/special/googlegroupsrep ly/>


                  PointedEars

                  Comment

                  • vadymus@gmail.com

                    #10
                    Re: How to compare 2 elements with same getElementById( x)?

                    Hello,
                    The code offered by RobG returns "Already have 0" alert and exists. It
                    does not find all similar values. Please, suggest something, if
                    possible...

                    function uniqueValues()
                    {
                    var val, vals = {};
                    var el, els = document.forms['add2db'].elements;

                    for (var i=0, len=els.length; i<len; ++i){
                    el = els[i];
                    val = el.value
                    if (vals[val]){
                    alert('Already have ' + val);
                    if (el.focus) el.focus();
                    return;
                    }
                    vals[val] = val;
                    }
                    }

                    Comment

                    • vadymus@gmail.com

                      #11
                      Re: How to compare 2 elements with same getElementById( x)?

                      Well, actually I was right, my version works except that it takes 5
                      minutes to check 192 fields. Any suggestions to improve code below?

                      function uniqueValues(){
                      var myForm = document.add2db ;
                      var iMax = myForm.imax.val ue;
                      for (i=0;i<iMax;i++ )
                      {

                      var fieldValue=docu ment.getElement ById(i).value;
                      //alert("id is "+document.getE lementById(i).n ame);
                      for(j=0;j<iMax; j++)
                      {
                      var fieldValue2=doc ument.getElemen tById(j).value;
                      if ((i!=j) && (fieldValue == fieldValue2)) { //FIX HERE
                      alert("I am sorry, but these values are identical:
                      "+document.getE lementById(j).v alue);
                      document.getEle mentById(j).foc us();
                      return false;
                      }
                      }
                      }
                      return true;
                      }

                      Comment

                      • Jonas Raoni

                        #12
                        Re: How to compare 2 elements with same getElementById( x)?

                        vadymus@gmail.c om escreveu:[color=blue]
                        > Well, actually I was right, my version works except that it takes 5
                        > minutes to check 192 fields. Any suggestions to improve code below?[/color]

                        As another guy said, it isn't right to have a field's id starting with
                        a number, so in the example bellow, I changed to t0, t1, t2...

                        <form id="add2db">
                        <input type="text" id="t0" value="0" />
                        <input type="text" id="t1" value="1" />
                        <input type="text" id="t2" value="2" />
                        <input type="text" id="t3" value="3" />
                        <input type="hidden" id="imax" value="4" />
                        <input type="button" onclick="unique Values();" value="Test" />
                        </form>

                        <script type="text/javascript">
                        function uniqueValues(){
                        for(var f = document.forms. add2db, i = f.imax.value, j, o; i; )
                        for(j = --i; j;)
                        if(f["t" + i].value == (o = f["t" + --j]).value)
                        return o.focus(), o.select(), alert("I am sorry, but the values are
                        identical."), false;
                        return true;
                        }
                        </script>

                        I hope it will help...

                        --
                        Jonas Raoni Soares Silva


                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: How to compare 2 elements with same getElementById( x)?

                          vadymus@gmail.c om wrote:
                          [color=blue]
                          > The code offered by RobG returns "Already have 0" alert and exists.
                          > It does not find all similar values. Please, suggest something, if
                          > possible...[/color]

                          Gladly. Would you please quote what you reply to and provide
                          attribution of quoted material?

                          <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>
                          <URL:http://www.safalra.com/special/googlegroupsrep ly/>
                          [color=blue]
                          > function uniqueValues()
                          > {
                          > var val, vals = {};
                          > var el, els = document.forms['add2db'].elements;
                          >
                          > for (var i=0, len=els.length; i<len; ++i){
                          > el = els[i];
                          > val = el.value
                          > if (vals[val]){
                          > alert('Already have ' + val);
                          > if (el.focus) el.focus();
                          > return;[/color]

                          Remove that line to prevent the function from exiting. Remove other lines
                          in that block if you do not need the respective feature.

                          If the code is to find all equal or similar values, I suggest you use an
                          Array for property value. Say you want to have an array of references of
                          all equal values in the form, you could do

                          var vals = {};
                          for (var i = 0, len = els.length; i < len; ++i)
                          {
                          var el = els[i], val = el.value;
                          if (vals[val])
                          {
                          vals[val].push(el);
                          }
                          else
                          {
                          // Creates a new Array object with the `el' reference as only
                          // element and assigns a reference to that Array object to a
                          // property of the Object object referred to by `vals';
                          // use `new Array(...)' where Array literals are not supported.
                          vals[val] = [el];
                          }
                          }

                          vals["0"] would then be a reference to an Array object encapsulating a list
                          of references to all form elements that have value "0". If instead you
                          want to find all similar values, you would need to define what you consider
                          to be similar first; then an appropriate hash function can provide for
                          mapping the similar value to a property.

                          Note that the inheritance caveat I mentioned before in
                          news:1369941.TJ G4NIPlZz@Pointe dEars.de would still apply.


                          PointedEars

                          Comment

                          • RobG

                            #14
                            Re: How to compare 2 elements with same getElementById( x)?

                            Jonas Raoni wrote:

                            [...]
                            [color=blue]
                            > <script type="text/javascript">
                            > function uniqueValues(){
                            > for(var f = document.forms. add2db, i = f.imax.value, j, o; i; )
                            > for(j = --i; j;)
                            > if(f["t" + i].value == (o = f["t" + --j]).value)
                            > return o.focus(), o.select(), alert("I am sorry, but the values are
                            > identical."), false;
                            > return true;
                            > }
                            > </script>
                            >
                            > I hope it will help...[/color]

                            But it's very inefficient, it will require n^2 checks to be performed
                            (where n is the number of inputs to check).

                            My first post simply stopped at the first match as designed. Thomas'
                            final version likely suits the OP better.


                            --
                            Rob

                            Comment

                            • RobG

                              #15
                              Re: How to compare 2 elements with same getElementById( x)?

                              Thomas 'PointedEars' Lahn wrote:

                              [...]
                              [color=blue]
                              > var vals = {};
                              > for (var i = 0, len = els.length; i < len; ++i)
                              > {
                              > var el = els[i], val = el.value;
                              > if (vals[val])[/color]

                              If there is a concern that vals[val] may be evaluated incorrectly, then:

                              if (val in vals)


                              will be better - it works with empty values too. I can't think why I
                              didn't go that way in the first place.


                              [...]


                              --
                              Rob

                              Comment

                              Working...