js get element from window['id'] vs document.getElementById('id') [duplicate] - javascript

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 4 years ago.
What is better and what is the difference (performance, browser support...)?
I thought that DOM elements that have an id automatically create in a window object so you don't need to find them in DOM through getElementById. Am I right and what problems it could bring? I can't find any documentation on such a possibility of js window object.
Examples with window:
window['elementId'];
window.elementId;
The same result as getElementById and querySelector

Related

Unable to get innerText with Javacript [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed yesterday.
I am attempting to get the innerText from the body of the website to print to the console and I am not sure what I am doing wrong.
The following code I have works with listing all content of the body:
console.log(document.getElementsByTagName('body'));
but when I use the following:
console.log(document.getElementsByTagName('body')[0].innerText);
I get an error and there's no output.
I was expecting for the console output to show the innerText of the body.

How to select a object by function? [duplicate]

This question already has answers here:
How to select Fabric.js object programmatically
(4 answers)
Closed 5 years ago.
I would like to select a object by run function instead of by mouse click. But I couldn't find the function from the docs for Fabric JS.
js
canvas.setActiveObject();
can't work in 2.0.0-beta.4 ,I solved it by change the version to 1.7.17.

Nested Webcomponents issue [duplicate]

This question already has an answer here:
Nested element (web component) can't get its template
(1 answer)
Closed 6 years ago.
I play with webcomponents using webcomponents.js and Shadydom polyfills.
My code is on Github : https://github.com/olofweb/webcomponents-action-bar
The first webcomponent is my-action-2. It is a button with a title. The second webcomponent is my-action-bar. The goal is to programmatically add my-action-2 into my-action-bar.
For this, my-action-bar has an addAction() method who create a new my-action-2 element and add it to the shadow DOM.
The problem is on my-action-2, line 33. Whenmy-action-2 is nested in my-action-bar, then the shadowRoot variable represent the shadow DOM of my-action-bar instead of my-action-2 !
It was a duplicate.
Github project updated if anyone is interested.

Are Assumed Variables Safe to Use? [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 7 years ago.
I've always noticed that Chrome assumes variables based upon elements within an HTML document that have an ID.
For example, if I had <video id="video" src="foo.webm"></video> on my page, the variable "video" would be created automatically by the browser. No need to type out var video = document.getElementById("video");
Is this a standard browser feature? Is it something that is here with us to stay? Should I take advantage of this or be wary of it? I like it because it saves precious bytes, especially when you have a ton of element objects to define.
Variables are not created that way.
document.getElementById("video") means you are selecting the "video" element, not variable. Checkout CSS Selectors

Does JavaScript have an order of precedence like CSS? [duplicate]

This question already has answers here:
load and execute order of scripts
(6 answers)
Closed 9 years ago.
Does JavaScript behave like CSS in that on-page scripts will take a higher precedence over external scripts? The reason I ask is because I am wondering if I can override an external script's commands to change a CSS attribute on expansion of a mobile nav bar.
You cannot really override anything that a previous script adds to a DOM element.
It will run, and then you just have to do something else to it.

Categories

Resources