8

Is it possible the read the price using the javascript

<span id="product_price_00873">
<span itemprop="price">178.00</span>
</span>

I just want to price 178.00. I can only using javascript.

Any suggestions will be appreciated.

jbcedge
  • 17,867
  • 28
  • 65
  • 89

2 Answers2

20

If you have the product element in product and you're using a modern browser, this should work:

var price = product.querySelector('[itemprop=price]').textContent;
icktoofay
  • 122,243
  • 18
  • 242
  • 228
10

Update:

Use Element.querySelector as described in icktoofay's answer, or perform the following:

const els = [...document.getElementsByTagName('span')];
props = els.filter(x => !!x.getAttribute('itemprop'));

// print the first element's price
console.log(props.shift().innerHTML)

Original:

var els = document.getElementsByTagName('span'), i = 0, price;

for(i; i < els.length; i++) {
    prop = els[i].getAttribute('itemprop');

    if(prop) {
        price = els[i].innerHTML;
        break;
    }
}
Alex
  • 34,121
  • 5
  • 74
  • 88