How to take this value inside the <div>

Asked

Viewed 35 times

2

The div is this:

<div class="table_box br_0_0_5_5 user_points" style="border-top:none;">4,660</div>

Code to get this div:

 document.getElementsByClassName("table_box br_0_0_5_5 user_points")[0]

How can I get the value "4,660" and put in an Alert? I tried several ways but did not succeed.

1 answer

0


Can use .textContent (captures the text inside the element):

var texto = document.getElementsByClassName("table_box br_0_0_5_5 user_points")[0].textContent;
alert(texto);
<div class="table_box br_0_0_5_5 user_points" style="border-top:none;">4,660</div>

But you also don’t need to include all classes in the method, with just one might be enough (if it’s the first page):

var texto = document.getElementsByClassName("br_0_0_5_5")[0].textContent;
alert(texto);
<div class="table_box br_0_0_5_5 user_points" style="border-top:none;">4,660</div>

  • One more question, if I want to do an if with that value, saying that if it is greater than or equal to 4,660 it shows Alert, how can I do this? Thank you

  • 1

    You can do it this way: var texto = document.getElementsByClassName("br_0_0_5_5")[0].textContent;&#xA;var valor = parseFloat(texto.replace(",",""));&#xA;console.log(valor);&#xA;if(valor >= 4660){&#xA; alert(texto);&#xA;}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.