При изменении текста label на jquery получаю ошибку

На страничке имеется label:

<label id="price-label" class="col-md-3 col-form-label" for="price">Price</label>

При попытке изменить текст с помощью jquery:

let price = $('#price-label').text();
price.text('Cost');

Получаю ошибку:

Uncaught TypeError: price.text is not a function

Как решить проблему?


Ответы (1 шт):

Автор решения: Igor

Ваша переменная price - строка.


$('#price-label').text('Cost');

или

let price = $('#price-label');
let oldPriceText = price.text();
price.text('Cost');

function changePriceToCost() {
  let price = $('#price-label');
  let oldPriceText = price.text();
  price.text('Cost');
  console.log(oldPriceText, "->", price.text());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label id="price-label" class="col-md-3 col-form-label" for="price">Price</label>

<br/>
<button onclick="changePriceToCost()">Change Price to Cost</button>

→ Ссылка