Kenneth B Kenneth B - 2 months ago 6
Javascript Question

Get `Meta` attribute `content` by selecting `property` attribute

In jQuery you can do this:

$("meta[property='fb:app_id']").attr("content");


Which will give you the
content
attribute value from the
meta
-tag with
property
attribute "fb:app_id".

How can I do this in plain ol' Javascript?

Thank you in advance. :-)

Kenneth

Answer

Not as elegant as JQuery I'm afraid...

var metaTags=document.getElementsByTagName("meta");

var fbAppIdContent = "";
for (var i = 0; i < metaTags.length; i++) {
    if (metaTags[i].getAttribute("property") == "fb:app_id") {
        fbAppIdContent = metaTags[i].getAttribute("content");
        break;
    }
}

console.log(fbAppIdContent);
Comments