getElementByID element is null [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Javascript element is null [duplicate]
(3 answers)
Closed 6 years ago.
In a scrip I try to get the width of an element like so:
var container = document.getElementById('content');
var cw = container.clientWidth;
But it gives me this error in the console:
Uncaught TypeError: Cannot read property 'clientWidth' of null
Executing the same statements in the console however, produces the desired value.
How is that possible?
Screenshot of the console:

This script is loading before the dom has finished loading (or in particular the div#content). ensure that this script is being inserted as the last element of the body (not in the head) or if div#content is being dynamically created, you may want to invest in jQuery's $(document).ready function

Related

Why is this returning null before returning actual element [duplicate]

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.

Uncaught TypeError: Cannot set property 'innerHTML' of null from external js file [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 5 years ago.
I am new to JS. I actually thought I could edit html pages from external js files. It works when i put my codes in functions. But outside that, i get this error.
Uncaught TypeError: Cannot set property 'innerHTML' of null
Guys, am i missing something?
Here is my JS code.
var One = document.getElementById("one");
var script = "Your message has been received. One of our reps will contact your shortly";
One.innerHTML = script;
This error typically occurs if you are trying to call a function in an object, but you typed the name wrong. please check it and follow dis rule.

document.querySelector returning null [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
I am trying to make my own version of jQuery. I am using this code so far, to test my selectors:
js(".hide");
function js(selector){
var applyTo = document.querySelector(selector);
applyTo.style.color = 'red';
}
However, this does not work, and the error console said: Uncaught TypeError: Cannot read property 'style' of null
Why is applyTo null?
To make it not fail in case the selector doesn't get you any element:
function js(selector){
var applyTo = document.querySelector(selector);
if(applyTo) { applyTo.style.color = 'red'; }
}
Make sure it's called only after the DOM has loaded.

Getting an uncaught typerror and don't know why [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
I am struggling with a little javascript.
I have these four lines:
var footer = document.getElementById('footer');
var cookieBanner = document.createElement('div');
cookiebanner.className = "cookieBanner";
footer.appendChild(cookiebanner);
But in console I get
Uncaught TypeError: Cannot read property 'appendChild' of undefined
If I do the same in console, it works. Why would that be?
var footer = document.getElementById('footer'); is returning undefined. Therefore when you try and call appendChild on undefined, you get:
Uncaught TypeError: Cannot read property 'appendChild' of undefined
If there is an element with that ID on your page, make sure your page is being rendered before this script is being ran.

Cannot use style method in JS [duplicate]

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.

Categories

Resources