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?
Related
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
This question already has answers here:
How to get the size of a JavaScript object?
(22 answers)
Closed 5 years ago.
I have this code:
var prop = $ ('body);
Does the 'prop' allocate more memory space or is it just referencing the element?
"prop" in your example is just a reference object. It only occupies space to store information like location of element in the DOM, element properties and inherited functions.
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')
This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Closed 8 years ago.
html code:
<div id="register"></div>
Javascript code:
console.log( register );//without even declaring the variable register
it returns <div id="register"></div> in the console.
Is that a normal behavior? (tested in Chrome and Firefox)
Is it documented somewhere?
Is it part of the ECMAScript specification?
This is DOM level 1, but doesn't wort in old IEs, IIRC.
This question already has answers here:
document.getElementById vs jQuery $()
(13 answers)
Closed 9 years ago.
Can somebody explain to me what the difference is between these two examples:
var obj = getElementById("id1"); // without jQuery
var obj = $("#id1"); // with jQuery
Is the returned value in both cases the same object?
getElementById() returns a DOM element only, $("#id1") returns a jQuery object containing DOM element(s).