Jquery Reimplementation [duplicate] - javascript

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

Related

Equivalent of jQuery's :contains for grabbing text in JavaScript [duplicate]

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?

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 ....

How to get value of span class? [duplicate]

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.

Is the dom automatically addressable in JavaScript? [duplicate]

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')

What is the difference between using jQuery and plain Javascript? [duplicate]

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).

Categories

Resources