Chrome error, says 'no method 'getElementsByClass' [duplicate] - javascript

This question already has answers here:
"document.getElementByClass is not a function"
(12 answers)
Closed 9 years ago.
I have the following line in my Javascript code
window.onload = function () { document.getElementsByClass("maintable")[0].width = "200%"; }
When I Try this, I get an error in Google Chrome saying "Uncaught TypeError: Object #{HTMLDocument} has no method 'getElementsByClass'" Can anyone tell me how to fix this?

It should be getElementsByClassName, not getElementsByClass.

Similar question here:
It should be getElementsByClassName:
window.onload = function () { document.getElementsByClassName("maintable")[0].width = "200%"; }

Related

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 months ago.
document.querySelector('.number');
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
console.log(guess, typeof guess);
if (!guess) {
document.querySelector('.message').textContent = 'No Number!';
}
});
I don't know what's wrong with the addEventListener, Can anyone please help me?Thank you
Looks like you are trying to access an element that is null. The dot operator throws an error when it's trying to access null.addeventlistener(...)
I am not sure if there is anything wrong with your HTML as its not been shared here. But my guess is it has to do with the HTML classes.
For debugging purposes print your document.querySelector('.check') and see what it consoles and based on that take it from there.

Why is there "undefined" in the console? [duplicate]

This question already has an answer here:
Why does this JavaScript code print "undefined" on the console?
(1 answer)
Closed 4 years ago.
Question about javascript from a rookie.
I need your help, thank you.
It says undefined because nothing get returned by the function sayHi ; and you are displaying the return of the function.
function sayHi() {
console.log('Hello');
}
console.log(sayHi());
function sayHi2() {
console.log('Hello');
return 'returned value';
}
console.log(sayHi2());
Because you console.log() your console.log().
You only need to call instructor.sayHi() or your sayHi function should return a string that you console.log().

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.

Javascript pointer to function [duplicate]

This question already has an answer here:
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 8 years ago.
Javascript is def not my main programming language I for practice I wanted to do something simple, at least i thought ;)
This is my code:
function Log () {}
Log.format = function (func, facility, text) {
func("hellow");
};
Log.debug = function(facility, text) {
Log.format(console.warn);
};
When I call Log.debug("we", "deb"); I get a Uncaught TypeError: Illegal invocation error. What am I doing wrong?
Depending on the browser, console methods can only be called in the context of console. Try Log.format(console.warn.bind(console));

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