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.

2
  • 2
    Are you using a framework such as jQuery, or just pure JavaScript? Also, can you post the code you tried that didn't work? Commented Mar 20, 2015 at 21:38
  • Well getElementByClass doesn't exist. Commented Mar 20, 2015 at 21:43

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

2

You can do that like so

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

FIDDLE

Comments

0

try getElementByClassName I believe is the proper syntax.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.