3

My question is in the title. I want to add a data attribute to <html> in vue.js (v2). The auto generated code does not provide any hint to do so neither do the docs, as far as I see.

The desired result should look like:

<html data-foo="bar">
  <body>
    <div id="app">
      <!-- .. -->
    </div>
  </body>
</html>

Can someone tell me how to accomplish this?

3
  • 5
    well, just put it there in your markup, or in code ... vue doesn't touch anything outside the root element (the id=app in this case) how would you do it if you were not using vue? Commented Aug 19, 2021 at 10:03
  • 2
    Now I feel stupid. Thanks! In my case there is the /public/index.html which will be used to inject the vue.js code. Commented Aug 19, 2021 at 10:13
  • 1
    you've learned something - think positive Commented Aug 19, 2021 at 10:15

1 Answer 1

2

I guess this will works:

document.documentElement.setAttribute('data-foo', 'bar')

ps: I use this line to change my theme from dark to light mode. I got this from this example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.