Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 303
curly_brackets Avatar asked Nov 19 '12 10:11

curly_brackets


2 Answers

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);
like image 177
mccannf Avatar answered Oct 15 '22 04:10

mccannf


document.querySelector('meta[property~="fb:app_id"][content]').content
like image 21
swade Avatar answered Oct 15 '22 05:10

swade