How can I get the value inside this td

3

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

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

Using the console:

document.querySelector(".monetary")

In this case I get the element:

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

But I need to get the value 75.22 isolated.

How do I proceed?

    
asked by anonymous 30.03.2016 / 17:25

2 answers

2

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

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

To apply to your case you can do this:

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

Example: link

    
30.03.2016 / 17:29
0

With jQuery you can do this:

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