0

I'm having trouble getting the exact price off of this html code :

<span id="price" data-selenium-price="1094.4">$1,094.40</span>

Below is what I used to get the exact price but it resulted in the value 1 rather than the value 1094.4 :

function() { 
    try { 
        return document.querySelector("#price").innerText.match(/^.{1}(.*).{0}/i)[1].trim(); 
    }
    catch(e) {
        return "";
    }
}

I'm not quite sure what I can do? Any help would be appreciated.

M0nst3R
  • 5,186
  • 1
  • 23
  • 36
JamesRocky
  • 721
  • 9
  • 23

4 Answers4

1

If the data-selenium-price and the innerText value is same, you can take out the attribute value.

function price() {
  try {
    console.log(document.querySelector("#price").getAttribute("data-selenium-price"));
  } catch (e) {
    return "";
  }
}
<span id="price" data-selenium-price="1094.4">$1,094.40</span>
<button type="button" onclick="price()">Get Price</button>
bhansa
  • 7,282
  • 3
  • 30
  • 55
  • It doesn't seem to be pulling anything using that unfortunately. Originally the code I used, it seemed as though it stopped showing anything after the "," in 1,094.40 - which is why I'm wondering now if I can remove the "," using the original code I used. – JamesRocky Oct 10 '17 at 13:37
  • As I said in the above answer, if you have always the attribute `data-selenium-price` available get the Attribute value instead of innerText if they are always the same. – bhansa Oct 10 '17 at 14:35
1

If you really do not want to use the attribute data-selenium-price then try this

console.log(parseFloat(document.getElementById("price").innerText.slice(1).replace(/,/g, '')));
 
<span id="price" data-selenium-price="1094.4">$1,094.40</span>

See the value in console

Optional
  • 4,387
  • 4
  • 27
  • 45
1

Try like this,

    
var price = document.getElementById("price").innerText.replace(/[$]/,'').replace(/,/,'');
console.log(parseInt(price));
<span id="price" data-selenium-price="1094.4">$1,094.40</span>
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

It looks like using this seemed to have solve my inquiry:

function() { 
    try { 
        return document.querySelector("#price").innerText.match(/^.{1}(.*).{0}/i)[1].replace(",", '').trim();
    }
    catch(e) {
        return "";
    }
}
JamesRocky
  • 721
  • 9
  • 23