0

Basically I'm gonna update this class' html but I keep getting the same error and have absolutely no idea why

document.querySelector(`.${clickField}`).innerHTML = `<img src ='${icon}.svg'>`

Uncaught DOMException: Document.querySelector: '.3-1' is not a valid selector

(I've seen similar questions but they all have jquery etc so i didnt understand shit from them)

Someone just said classes cant start w numbers he's right im so dumb lmfao image of 3-1 existing in DOM

Ase Last
  • 7
  • 6

1 Answers1

0

I'm assuming clickField is supposed to be the class of the element you want to get. In that case, since the class starts with a digit, the easiest way to select it using CSS selector is using attribute selectors, like so:

document.querySelector(`[class="${clickField}"]`).innerHTML = `<img src ='${icon}.svg'>` 

If clickField might contain " characters, you need to replace ${clickField} by ${clickField.replace(/"/g, '\\"')} to escape them properly.

There is also a far easier way, using document.getElementsByClassName():

document.getElementsByClassName(clickField)[0].innerHTML = `<img src ='${icon}.svg'>`

Classes may contain any characters except space, as the HTML standard imposes no restrictions on them.

D. Pardal
  • 6,173
  • 1
  • 17
  • 37