Home > front end >  How to get a meta tag content value and do a substraction
How to get a meta tag content value and do a substraction

Time:11-23

I want to grab the meta tag content value and then do a substraction.

Can anyone help me please?

The meta tag looks like this:

<meta itemprop="price" content="1699">

I tried this but it does not work:

var oldprice = document.body.querySelector("[itemprop~=price][content]").content;

var newprice = oldprice - 500;

CodePudding user response:

As per @Gicu Aftene comment. Meta tags are in head of document.
Doing document.head. didn't work with querySelector even though document.head does return head.

// document.head... doesn't work
let oldprice = document.querySelector('meta[itemprop="price"]').content;
let newprice = oldprice - 500;

console.log(newprice)
<meta itemprop="price" content="1699">

  • Related