How can I get the value inside this <td>

Asked

Viewed 73 times

3

Hello. I need to get the value inside a <td> through its selector.

This <td> contains the total amount of a purchase (within a checkout).

Using the console:

document.querySelector(".monetary")

in this case I can get the element:

<td class="monetary" data-bind="text: totalLabel">R$ 75,22</td>

however I need to take the value 75,22 isolated.

How should I proceed?

2 answers

2


Generally to capture content in a string you can use regular expressions. In this case: 'R$ 75,22'.match(/\d+,\d+/) would-be ["75,22"].

In regular expressions the \d means digit, the + means 1 or more of the previous selector.

To apply in your case you can do so:

var td = document.querySelector(".monetary");
var preco = td.innerHTML.match(/\d+,\d+/)[0];

Example: https://jsfiddle.net/3cxooxss/

  • 1

    Thank you. Working...

0

With jQuery you can do this way:

var valorTd = $(".monetary").text(); // retorna R$ 75,22
var valorMonetario = valorTd.replace("R$ ", ""); // retorna 75,22

Browser other questions tagged

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