How do I get the value of a child div

Viewed 45

Good day, I would like to ask how I can get the value from price. Neither of them has a unique id so it makes things hard for a noob like me. Any suggestions? Thanks.

<div class="modal">
    <span class="name">$</span>
    <span class="price">0.04</span>
    <hr style="background: linear-gradient(to right, #ffc82d , #ffff73);">
    <div class="weapon" style="background-image: url('assets/img/free_case_item.png');"></div>
</div>
2 Answers

You can try with document.querySelector:

var price = document.querySelector("div.modal span.price").innerText

This matches the first <span class="price"> inside <div class="modal">.

You can do it with pure JavaScript using DOM manipulation.

Assuming you only have one element with the price class: document.getElementsByClassName("price").item(0).innerText.

Ideally you would give it an ID (e.g. price): document.getElementById("price").innerText

Related