javascript – How can I get the value inside this<td>

Question:

Hi. 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 can get the element:

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

however I need to get the value 75.22 alone.

How should I proceed?

Answer:

Generally speaking 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 \d means digit, + means 1 or more of the previous selector.

To apply in your case, you can do this:

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

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

Scroll to Top