John P. John P. - 2 months ago 24
Javascript Question

Populate Meta tag in Javascript

I am trying to get the data value in the meta tag below

<meta data-my-id="home">


So far i tried this, but this is actually returning the html.

$("meta[data-my-id]");


Would someone be able to help me with it?

Answer

Using has attribute selector you can get the element with a certain attribute then get it's value using data() method.

$("meta[data-my-id]").data('my-id');

or use attr() method

$("meta[data-my-id]").attr('data-my-id');