0
<div class="basePrice">$99.99</div>

I want to change the value $99.99 using JavaScript.

I tried using getElementByClass but it didn't produce the results that I was hoping to get.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
software is fun
  • 7,286
  • 18
  • 71
  • 129

3 Answers3

3

document.getElementsByClass returns a NodeList, which is kind of like an array.

You have to specify which element (there's only one here, so I'm assuming the first) you're looking for, and then you can use .textContent to change the text of the node.

document.getElementsByClassName("basePrice")[0].textContent = "$49.99";

document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
<div class="basePrice">$99.99</div>
AstroCB
  • 12,337
  • 20
  • 57
  • 73
2

You can do that like so

document.querySelector('.basePrice').innerHTML = 'different text'

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0

try getElementByClassName I believe is the proper syntax.

JoeL
  • 710
  • 4
  • 18