-2

I want to change:

<b class="added-points">0</b>

in my website using javascript. I searched and found

document.getElementById("added-points").innerHTML = "9999999999999";

but it is for id and I search for class. How to do it?

takendarkk
  • 3,347
  • 8
  • 25
  • 37
halkunex
  • 13
  • 2
  • 1
    `class` != `id`. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName – ps2goat Mar 09 '18 at 22:10
  • 7
    Possible duplicate of [How to get element by class name?](https://stackoverflow.com/questions/17965956/how-to-get-element-by-class-name) – esqew Mar 09 '18 at 22:10

3 Answers3

1

Simply use document.querySelector(".added-points").

Md. Abu Taher
  • 17,395
  • 5
  • 49
  • 73
1

If you use

document.getElementsByClassName("added-points")[0].innerHTML = "9999999999999";
<b class="added-points">0</b>

you will set the first ([0]) element in the array of elements with the class added-points to 9999999999999. Note document.getElementsByClassName gives you an array result even if there is just one item found.

Joe Wilson
  • 5,591
  • 2
  • 27
  • 38
0

in chrome

document.querySelector(".added-points").innerHTML = "9999999999999";

in IE

Array.prototype.forEach.call(document.getElementsByTagName('*'), item => {
  if (item.className.split(' ').indexOf('added-points') > -1) {
    item.innerHTML = '9999999999999'
  }
})
limi58
  • 1
  • 2