SajZ
SajZ

Reputation: 260

Jquery to get or retrieve data from meta tags

Meta tag is unnamed. I'm unable to get its content info

I have tried for meta tag with name, description and it worked. I tried to call all meta without specifying name and description. But it was hard to locate the data i was searching for due to lot of metas. I need to filter with 'itemprop'.

<meta itemprop="url" content="https://examples.com/">
var desc = $('meta').attr('content', 'value');
console.log(desc);
//This returned all metas with attr content
// I wish to filter meta which has itemprop and its value is url

output :- https://examples.com/

Upvotes: 0

Views: 567

Answers (2)

AMAN BHARDWAJ
AMAN BHARDWAJ

Reputation: 89

Use followoing in case

  • To set the attribute value : $('meta').attr('content', 'value');
  • To get the attribute value : $('meta[name="description"]').attr('content');
  • To filter base on attribute (if itemp prop is present in meta tag) : $('meta[itemprop]')

enter image description here

Thank you for reading

Upvotes: 0

fdomn-m
fdomn-m

Reputation: 28621

You can loop through them all and check itemprop, example:

$('meta').each(function() {
  if ($(this).attr('itemprop') == "url") {
      console.log($(this).attr("content"));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta itemprop="not found" content="https://examples.com/">
<meta itemprop="url" content="https://examples.com/">
<meta itemprop="test" content="https://examples.com/">

Or you can use attribute filter:

console.log($("meta[itemprop='url']").attr("content"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta itemprop="not found" content="https://examples.com/">
<meta itemprop="url" content="https://examples.com/">
<meta itemprop="test" content="https://examples.com/">

Upvotes: 1

Related Questions