3

I have the following in HTML code:

<meta name="citation_journal_title" content="Psychological Bulletin" />

It is quite easy to get the content by using:

document.getElementsByName("citation_journal_title")[0].getAttribute("content")

However, I cannot deal with this:

<meta property="og:site_name" content="APA PsycNET" />

How do you retrieve the content of og:site_name? I am aware of the question How do I get the information from a meta tag with javascript? but I'm looking for something quite simple like

document.getElementsByName("citation_journal_title")[0].getAttribute("content")
Community
  • 1
  • 1
menteith
  • 596
  • 14
  • 51
  • Possible duplicate of [Find an element in DOM based on an attribute value](http://stackoverflow.com/questions/2694640/find-an-element-in-dom-based-on-an-attribute-value) – chiliNUT Oct 09 '16 at 17:59

2 Answers2

10

You need to use attribute selector [attr=value] to do this work. Use it in querySelector() like this

var attr = document.querySelector("meta[property='og:site_name']").getAttribute("content");
console.log(attr);
<meta property="og:site_name" content="APA PsycNET" />
Mohammad
  • 21,175
  • 15
  • 55
  • 84
-1

In Jquery, you can use attr()

$('meta[property="og:site_name"]').attr('content')

In JavaScript, you can use querySelector

querySelector is supported by all modern browsers, and also IE8.

var element = document.querySelector('meta[property="og:site_name"]');
var content = element && element.getAttribute("content");
console.log(content);

References

console.log($('meta[property="og:site_name"]').attr('content'));

var element = document.querySelector('meta[property="og:site_name"]');
var content = element && element.getAttribute("content");
console.log(content);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta property="og:site_name" content="APA PsycNET" />
Mohit Tanwani
  • 6,608
  • 2
  • 14
  • 32