0

I have a line <meta property="product:price:amount" content="3.05"/> in a large html file.

I need to store the value of content inside a variable, so that I may access it globally. How can I do that?

3
  • Have have you tried so far? Commented Jul 21, 2017 at 14:29
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please see here to learn how to write effective questions. Commented Jul 21, 2017 at 14:30
  • jQuery: How to get the value of an html attribute? Commented Jul 21, 2017 at 14:31

4 Answers 4

3

Just catch it with querySelector to get it's content attribute.

const content = document.querySelector('meta').content;

console.log(content);
<meta property="product:price:amount" content="3.05"/>

In case of multiple meta tags:

const elems = document.querySelectorAll('meta');

let content = Array.from(elems).find(v => v.content).content;

console.log(content);
<meta property="product:price:amount"/>
<meta property="product:price:amount"/>
<meta property="product:price:amount" content="3.05"/>

Sign up to request clarification or add additional context in comments.

4 Comments

that would not be very specific considering you have multiple meta elements right?
@hansTheFranz That's a higher level of abstraction, which requires another solution. In this specific case, that's a complete needed solution.
What's the need for let content = elem.content;. Why can't I just do console.log(elem)
@kashish Then you will log whole meta element. If you really want to log it - console.log(elem.content).
2

To very specifically get the meta tag you are after (event if there are multiple meta tags):

var variable = document.querySelectorAll('meta[property="product:price:amount"]')[0].content;

2 Comments

Great answer but why .querySelectorAll() to only set [0], would it not be just as easy to use document.querySelector('meta[property="product:price:amount"]').content;
absolutely! habit i guess
0

If there is only one item then simply

document.getElementsByTagName("meta")[0].content

Comments

0

For a single meta tag:

var myGlobal = document.querySelector('meta[content]').getAttribute('content');
document.body.textContent = myGlobal;
<meta property="product:price:amount" content="3.05"/>

If you have a lot of tags:

var contentArray = [];
document.querySelectorAll('meta[content]').forEach(function(meta){
  contentArray.push(meta.getAttribute('content'));
});
document.body.textContent = contentArray.join(' - ');
<meta property="product:price:amount" content="3.05"/>
<meta property="product:quality:amount" content="9.25"/>
<meta property="product:id:amount" content="1.0"/>

If you want to be more specific about the tag you can change the selector:

...querySelector('meta[property="product:price:amount"][content]')...

Read more about selectors https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors

5 Comments

updated the answer to handle both cases. he does say that he have "a line", but it's there to help other ppl.
I mean other meta tags such as: Description, Keywords, Author ect.... <meta name="description" content="Meta tag exmaple"> => <meta name="keywords" content="Meta, example"> => <meta name="author" content="John Smith">
the updated code will handle all meta tags that have a content attribute.
Exactly my point. I don't think the OP wants the variable to hold the description, author and other meta tag data...
then all they gotta do is to change the selector. not a big deal I guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.