Reputation: 3
I have a problem. I am trying to create some javasript, and it works perfectly in Google chrome, but when I tried to use it in Mozilla Firefox I am getting errors.
for example:
var x = document.getElementsByClassName("some_class");
x[0].innerHTML = 'some html code';
works perfectly in Chrome, but firefox returns this error in browser console:
TypeError: x[0] is undefined
also another problem:
var test = document.getElementsByClassName("some_class2");
test.remove();
returns this error in Firefox:
TypeError: test.remove is not a function
everything works perfectly in Chrome, and in Tampermonkey for example.
I will appreciate any help.
Upvotes: 0
Views: 136
Reputation: 114
It's fairly simple.
The browser is telling you that x[0] IS UNDEFINED (doesn't exists) therefore it doesn't have a innerHTML
property. Probably because the script is being executed before the elements with that class are in the DOM.
Try putting your script at the end of the document, before the </body>
tag.
The second case is pretty much the same. It's the browser is telling you the test variable doesn't have a remove method.
document.getElementsByClassName
returns a HTMLCollection
. HTMLCollection doesn't have a remove method.
You are probably trying to remove an element from that collection. In that case you can access that element like this test[index].remove()
Upvotes: 0
Reputation: 22412
I just tested on Firefox and it works perfectly, how do you get such a result?
"use strict";
var x = document.getElementsByClassName("some_class");
x[0].innerHTML = 'some html code';
<div class="some_class">some_class 1</div>
<div class="some_class">some_class 2</div>
Upvotes: 1