Equality?

Dear All,

I am digging myself into a hole here over what I think should be a very simple bit of code. Perhaps I have been looking at too long have have lost my way.
Either way some direction would be most welcome.

I want to use a regular expression to differentiate between elements. So I am assuming (bad word that) that I need to convert a reference to an element intotext before I can test it using a regular expressions.

i.e

var myString "<a id=“ml2” ";
var myExpr1 = /ml2/;

console.log(“test text”,myExpr1.test(myString)); // displays true

whereas

var myElement = document.getElementById(“ml2”);

console.log(“element”,myElement); // displays element<a id=“ml2” href=“http://localhost/xxxxhome/search.html”>

console.log(“test element”,myExpr1.test(myElement)); displays false

so I am assuming (ekk there’s that word again) when the myElement is output or referenced by any other code it is not as a string.

Therefore I want to know how to make it into a string. toString(myElement); returns [object Window] which was not what I was expecting

==================================================================================================

I am also very confused with equality of elements:-

var myElement = document.getElementById(“ml2”);

myElement.onmouseout = onMouseOut;

function onMouseOut(event){

          console.log("element",myElement);               // displays element&lt;a id="ml2" href="http://localhost/xxxxhome/search.html"&gt;
          console.log("target",event.target);                 // displays target&lt;a id="ml2" href="http://localhost/xxxxhome/search.html"&gt;

          if (myElement != event.target){
                 console.log("not equal");                      // always unequal
          } else {
                 console.log("equal");
          }

}

Why are they always unequal they report identically is it something like they are difference instances of the same reference???

Because “Two objects are strictly equal if they refer to the same Object.”

Keep in mind that by “Object” they don’t mean the element itself.

Hi Paul,

Ok I am still not quite with this:

  1. We are saying object and element might not be the same thing. Are we talking object as in DOM object. My understanding was that an object could be more or less anything within the DOM. And that Element was one of the 12 node types within the DOM. But there is no getObjectById there is only a getElemetById. So I confess to remaining unsure as to the difference in JS terms between the object which I presume contains the element and the element itself.

  2. Are we saying that equality between objects can only ever be strict equality i.e. we should be using === or !== ?

  3. If I am testing for the id of the element should I be using myElement.id ?

Many Thanks