SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: equality ?

  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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<a id="ml2" href="http://localhost/xxxxhome/search.html">
    console.log("target",event.target); // displays target<a id="ml2" href="http://localhost/xxxxhome/search.html">

    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???

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by codepuppy View Post
    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."
    https://developer.mozilla.org/en/Jav...ison_Operators

    Keep in mind that by "Object" they don't mean the element itself.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •