0

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.

carlosbvz
  • 163
  • 2
  • 14
  • Possible duplicate of [Why don't we just use element IDs as identifiers in JavaScript?](http://stackoverflow.com/questions/25325221/why-dont-we-just-use-element-ids-as-identifiers-in-javascript) – Alexander O'Mara Dec 18 '15 at 23:22
  • that's a feature, not a bug. it can be handy, but you need to know about it (and it's ups and downs). on simple SPAs with well-named IDs (selUsers, tblHistoryGrid, inpPassword, etc), it can eliminate long-winded DOM methods or the need for a dom library. – dandavis Dec 18 '15 at 23:31

1 Answers1

1

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,

Community
  • 1
  • 1
Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45
  • Thanks a lot louy, and my next question was: do they become global... and the answer is right in the link you shared (Y). – carlosbvz Dec 18 '15 at 23:34
  • You're welcome. In browsers, `window` is the global object. Anything under `window` is a global variable. – Louay Alakkad Dec 19 '15 at 12:01