-1

How do I insert HTML to a div in a tampermonkey script? I've tried

var htmlll= `
<button>hi</button>
`
document.getElementsByClassName("clasname").innerHTML += htmlll;

Although it keeps saying Uncaught (in promise) TypeError: Cannot read property 'innerHTML' of null.

I have tried

document.addEventListener("DOMContentLoaded", function(event) {
var htmlll= `
<button>hi</button>
`
document.getElementsByClassName("clasname").innerHTML += htmlll;

});

to counter this error, but it didn't work. It didn't give me an error and it didn't append the html.

Note: I am using vanilla javascript. Please dont answer with jquery solutions.

mcdonalds291
  • 2,024
  • 1
  • 5
  • 14

2 Answers2

0

It should be document.querySelector('.jspath') or '#jspath' where you mistakenly wrote only jspath

Joy Dey
  • 563
  • 1
  • 5
  • 17
0

It needs to be something like

document.getElementsByClassName("clasname")[0].innerHTML += htmlll;

since .getElementsByClassName() returns a collection, not a single element.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43