Home > Net >  Convert querySelector() value to boolean
Convert querySelector() value to boolean

Time:11-10

I'm trying to read the value of a HTML meta tag:

<meta name="accepted-cookies" content="false" />

From a react component (using Typescript) I want to read the value as a boolean:

const metaValue = (document.querySelector('meta[name="accepted-cookies"]')).value === "true";

But I'm getting the error TS2339: (JS) Property 'value' does not exist on type 'Element'.

CodePudding user response:

You should use getAttribute to avoid having to cast (assert) the result to HTMLMetaElement:

const metaValue = document.querySelector('meta[name="accepted-cookies"]')!.getAttribute("content") === "true";
//                                                                       ^? non-null assertion
  • Related