0

I want to use the HTML-Symbols &#9650 and &#9660 that stand for "arrow up" and "arrow down" in the <script> section of my Vue.js component. I know that something simple as the following doesn't work. But I tried different functions that I found on the internet and nothing worked.

this.$refs["span-1"].textContent = "&#9650";
4
  • 2
    Try innerHTML instead of textContent. Commented Sep 28, 2022 at 18:06
  • Thanks a lot. I can't believe I wasted so much time on this. Commented Sep 28, 2022 at 18:11
  • 2
    I assume you are trying to toggle this arrow at some point. You already got your answer, but if I may suggest: Do those with CSS instead of using $refs. Usually, it is not a good practice to modify the HTML that way. Commented Sep 28, 2022 at 22:42
  • Thank you for the answer. I could also do it with CSS. I want to change the content of the <span> with three different strings depending on what I click. What comes to my mind is that I need 3 different CSS classes to achieve that. Is there a simpler way? Is $refs a better choice here and why is it not a good practice in general? Commented Sep 29, 2022 at 8:08

1 Answer 1

1

There are two ways to achieve this :

  1. Use v-html directive

new Vue({
  el: '#app',
  data: {
    content: "&#9650"
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p v-html="content"></p>
</div>

  1. Use innerHTML instead of textContent

new Vue({
  el: '#app',
  mounted() {
    this.$refs.myTag.innerHTML = "&#9650";
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p ref="myTag"></p>
</div>

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.