javascriptdommeta-tags

How to retrieve meta tag data?


What I am trying to do is get a certain meta tag.

Looking for something similar to this:

<meta name="age" data-userid="number">

var meta = document.getElementsByTagName('meta');
var id = meta.substring(34, 37);
console.log(id);

I am trying to get the data-userid value. The other questions do not help because they are only asking how to get the content of the meta.

My code from one of the answers (still doesn't work):

var idl = localStorage.getItem("id");
if (typeof idl !== 'undefined' && idl !== null){}else if (typeof idl == 'undefined' || idl == null){
    localStorage.removeItem("id");

    var meta = document.getElementsByTagName('meta')[0];
    var idget = meta.getAttribute("data-userid");
    var id = idget;
    localStorage.setItem("id", id);
}


Solution

  • You can use getAttribute() method in the specific element to retrieve values.

    var meta = document.getElementsByTagName('meta');
    console.log(meta[0].getAttribute("data-isunder13"));
    <meta name="age" data-isunder13="some value">