Is the dom automatically addressable in JavaScript? [duplicate] - javascript

This question already has answers here:
Why don't we just use element IDs as identifiers in JavaScript?
(5 answers)
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 5 years ago.
It seems like these two statements produce the same output.
console.log(myId)
console.log(document.getElementById('myId'))
<div id="myId">Hello World!</div>
Q: Is it safe to use myId instead of document.getElementById('myId')

Related

Check condition for imbricated property in object [duplicate]

This question already has answers here:
Using optional chaining operator for object property access
(2 answers)
Access Javascript nested objects safely
(14 answers)
Closed 7 months ago.
I want to use the following condition
if (this.solvedConflictsDetails[this.instance.instanceId][this.model.name][this.entity.entityId].solved)
The problem is that if one of the above doesn't exist in the object it will say cannot read undefined
How would you check a condition like this?

Jquery Reimplementation [duplicate]

This question already has answers here:
Select all elements with a "data-xxx" attribute without using jQuery
(8 answers)
Find an element in DOM based on an attribute value
(11 answers)
How to get element by class name? [duplicate]
(4 answers)
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 12 months ago.
hey would love to have a take from a more experienced coder about this.
Would appreciate if you could give it to me in the below code format, thanks a lot
You could use document.querySelectorAll.
This returns a Node List, to convert that to an Array, you could do
const elements = [...document.querySelectorAll(".my-selector")];
That's called Vanilla Javascript
An example
var myArray = document.getElementsByClassName('myClass')
myArray will be a HTMLCollection

what's the role of slice method here ? why need it? [duplicate]

This question already has answers here:
Why do Array.prototype.slice.call(nodeList) for DOM elements?
(2 answers)
Explanation of [].slice.call in javascript?
(9 answers)
Closed 2 years ago.
In ES5 if have a NodeList and want to loop through it must first convert it to Array with slice and call. I can't understand correctly how this statement works. I need explanation.
let nodeList = document.querySelectorAll('div');
let nToArr = Array.prototype.slice.call(nodeList);
// Then Loop Through it ....

html element id treated as variable name [duplicate]

This question already has answers here:
Is there a spec that the id of elements should be made global variable?
(5 answers)
Javascript: access DOM elements without getElementById [duplicate]
(2 answers)
Accessing a div using id without getElementById and jQuery [duplicate]
(3 answers)
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 5 years ago.
I noticed that when I assign some id to a DOM element, I can then reference that element in javascript, just by treating its id as a javascript variable name.
For example
console.log(myDiv);
<div id="myDiv">some text</div>
Is there a difference between referencing a DOM element with document.getElementById(id) and just its id?
Can I expect this behaviour in all browsers?

node-hashtable - how to get set of all keys? [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 8 years ago.
In Java it's called the keyset.
https://www.npmjs.org/package/node-hashtable
how come there is no method to return a list of all the keys?
Looking at the source there seems to be a method indexes which returna a list of the keys.

Categories

Resources