Adam Halasz
Adam Halasz

Reputation: 58311

Javascript: Attributes on Element Request

If I get an element with document.getElementById(),

does javascript pull all the attributes of the element after I assigned to the variable object, or just when I request it by example: object.id?

var object = document.getElementById("object");

object.id;
object.value;

Upvotes: 1

Views: 327

Answers (3)

Danilo Bargen
Danilo Bargen

Reputation: 19452

object is a reference to the object itself. It does not pull/copy any values.

You can even try that by modifying a referenced object (example works here on SO):

> var object = document.getElementById("hlogo")
> object.id
"hlogo"
> document.getElementById("hlogo").id = "foobar"
"foobar"
> document.getElementById("hlogo").id
TypeError: Cannot read property 'id' of null
> object.id
"foobar"

Upvotes: 2

RoToRa
RoToRa

Reputation: 38421

Neither. The variable object is just a reference to an already existing object.

Upvotes: 3

Pointy
Pointy

Reputation: 413757

It doesn't really "pull" anything. The browser gives the JavaScript interpreter a reference to an object that, in ways peculiar to each browser, essentially is the DOM element. Accessing properties on DOM elements is pretty cheap, though modifying properties can have significant side-effects (which is usually why you'd want to modify a property from JavaScript, of course).

Upvotes: 3

Related Questions