4

I have the following code:

HTML

<div id="app">
  <h1>
  Hello {{superscript('hello')}}
  </h1>
</div>

JS

new Vue({
  el: "#app",
  data: {
  },
  methods: {
    superscript(input) {
        return '<sup>' + input + '</sup>'
    }
  }
})

I want this to render:

Hello hello

But instead it renders the tags themselves without turning it into a superscript. JSfiddle: http://jsfiddle.net/agreyfield91/eywraw8t/188244/

Is there a way to add html tags through a Vue.js method?

4
  • you need change your span to this: <span v-html="superscript('hello')"></span> Commented Jul 19, 2018 at 19:43
  • v-html ...we use..... v-html for inner html ... Commented Jul 19, 2018 at 19:44
  • please check this: jsfiddle.net/bzsjwx5c/2 Commented Jul 19, 2018 at 19:44
  • and read the documentation: vuejs.org/v2/guide/syntax.html Commented Jul 19, 2018 at 19:44

1 Answer 1

11

Instead of rendering the html, you need to bind it:

{{ result }}  => <span v-html="result"></span>

In your case:

<div id="app">
  <h1>
  Hello <span v-html="superscript('hello')"></span>
  </h1>
  <h1>
  What I want it to look like:   Hello <sup>hello</sup>
  </h1>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I need it to all be in the method though. Is it possible to do this without editing the html?

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.