I have the following meta tags on my website:
JavaScript
x
4
1
<meta name="description" content="content here" />
2
<meta itemprop="description" content="content here">
3
<meta property="og:description" content="content here" />
4
All 3 of these meta properties are necessary on my site. However, is there a way to reference a single description in a JS variable that I can pass to each of these content attributes?
Advertisement
Answer
You can use the querySelector with meta[key=value] and then set Attribute with setAttribute(key,value).
JavaScript
1
7
1
document.querySelector("meta[name='description'" ).setAttribute("content", "some new meta description");
2
3
document.querySelector("meta[itemprop='description'" ).setAttribute("content", "some new meta description");
4
5
document.querySelector("meta[property='description'" ).setAttribute("content", "some new meta description");
6
7