Search code examples
jquerymeta-tags

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/


Solution

  • 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/">