Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Associate an Object with a DOM Element

I have a master object in my JS setup, i.e.:

var myGarage = {
    cars: [
        {
            make: "Ford",
            model: "Escape",
            color: "Green",
            inuse: false
        },
        {
            make: "Dodge",
            model: "Viper"
            color: "Red",
            inuse: true
        },
        {
            make: "Toyota",
            model: "Camry"
            color: "Blue",
            inuse: false
        }
    ]
}

Now I loop over my cars and put them in a table. In the table I also have a button that lets me toggle the car as "in use" and "not in use".

How can I associate the DOM Element of every row with its corresponding car, so that if I toggle the "inuse" flag, I can update the master object?

like image 308
Steve Avatar asked Apr 19 '13 20:04

Steve


People also ask

How do you access elements using DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. const demoId = document.

Is a DOM element an object?

The DOM is the way Javascript sees its containing pages' data. It is an object that includes how the HTML/XHTML/XML is formatted, as well as the browser state. A DOM element is something like a DIV, HTML, BODY element on a page. You can add classes to all of these using CSS, or interact with them using JS.

How do I identify a DOM element?

The easiest way to find an HTML element in the DOM, is by using the element id.


1 Answers

You can actually attach an object directly to a node:

var n = document.getElementById('green-ford-escape');
n.carObject = myGarage.cars[0];
n.onclick = function() {
  doSomethingWith(this.carObject);
}

For the same of removing ambiguity, in some cases, it's more clear write the above event handler to refer to n instead of this:

n.onclick = function() {
  doSomethingWith(n.carObject);
}

You can also refer directly to the object from the attached event:

var n = document.getElementById('green-ford-escape');
n.onclick = function() {
    doSomethingWith(myGarage.cars[0]);
}

In the latter case, myGarage does not have to be global. You can do this and expect it to work correctly:

(function(){

    var myGarage = { /* ... etc ... */ };

    var n = document.getElementById('green-ford-escape');
    n.onclick = function() {
        doSomethingWith(myGarage.cars[0]);
    }

})();

The node's event function will "hold onto" the local variable correctly, effectively creating a private variable.

You can test this in your Chrome/FF/IE console:

var o = {a: 1};
var n = document.createElement('div');
n.innerHTML = "click me";
n.data = o;
n.onclick = function() { n.data.a++; console.log(n.data, o); }
document.body.appendChild(n);

You should see the console log two identical objects with each click, each with incrementing a values.

Beware that setting n.data to a primitive will not create a reference. It'll copy the value.

like image 186
svidgen Avatar answered Sep 29 '22 14:09

svidgen