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.
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:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 1 year ago.
I want to remove element with data-loc-id='2218'. How can I do this?
I tried this:
document.querySelectorAll(`[data-loc-id='2218']`).remove();
Error: Uncaught TypeError: document.querySelectorAll(...).remove is not a function
querySelectorAll() returns collection, you should loop through them and remove one by one:
document.querySelectorAll(`[data-loc-id='2218']`).forEach(el => el.remove());
OR: If you have single element with the attribute value then use querySelector() which returns the first matched element in the document:
document.querySelector(`[data-loc-id='2218']`).remove();
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.
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).
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 9 years ago.
In this code:
<header class="alg">Some text</header>
<script>
var header = document.getElementsByClassName("alg");
header.style.color = 'red';
</script>
after run it. I got from log:
TypeError: header_m.style is undefined
What do I do wrong?
getElementsByClassName returns multiple elements.
Therefore, you are accessing it improperly. What you want, in this case, is:
header[0].style.color = 'red';
// ^ [0] will get the first element with the class,
// which is in this case your is header element. [1] would get the second, etc.
jsFiddle.