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).
Related
This question already has answers here:
Native javascript equivalent of jQuery :contains() selector
(6 answers)
How to get element by innerText
(18 answers)
Closed 4 months ago.
Using jQuery, if I want to grab an element using the text inside it, I can do:
$( "div:contains('John went to the shop')" )
I can find loop functions and the use of XPATH, but nothing similar to jQuery.
Is there something simple like this for querySelector or equivalent methods?
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:
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 ....
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 2 years ago.
I want to assign the date to a variable I have tried with
var $x = document.getElementsByTagName("span").innerHtml but this assigns null value
You can get value with selector too.
document.querySelector("{SELECTOR}").innerHTML;
You can copy any selector with right click from Chrome Developer Console.
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
Why is this returning null before returning the actual element?
const subNav = document.querySelector('.subnav-block');
console.log(subNav);
Here's the codepen, lines 30 and 31
The null is logged even before the dom is rendered, hence couldn't find the element.