carlosbvz
carlosbvz

Reputation: 162

How is my JS code accesing DOM elements without me explicity caching them first?

Can someone help me understand why the textarea is being updated without caching it first

In my html I have a

<textarea id="inputData"></textarea>

and a reference to my js file which is:

(function(){
    "use strict"
    var analyzer = {
        text : [],
        init : function(){
            this.cacheDOM():
            this.render();
        },
        cacheDOM : function(){
           // Here is where I want to cache a portion of my DOM 
        },
        render : function(){
            inputData.value         = "my  data";
        }

    }// End analyzer

    analyzer.init();

})();

The result is the textarea being updated by the js file (render function), and I don't know/understand why this is happening without me caching the DOM first...

PS: This happens with the "id"s property and not using "class"es.

Upvotes: 0

Views: 30

Answers (1)

Louay Alakkad
Louay Alakkad

Reputation: 7408

All html elements with ids are added to javascript to the window object as attributes. See this for more details.

The reason is backward-compatibility I guess. Welcome to the world of front-end development,

Upvotes: 1

Related Questions