F
F
Fluber2020-12-27 13:11:58
Vue.js
Fluber, 2020-12-27 13:11:58

How to catch focus on an element using vuejs?

Hello, I have this code:

<template>
  <span
    tabindex="-1"
    contenteditable="true"
    spellcheck="false"
    style="width: 800px"
    v-text="displayText.join('')"
    @focus="focused = true"
  />
</template>


I'm trying to get focus, but for some reason the native focus event doesn't fire on the span element. The value of "focused" does not change, always false. How can this be resolved?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-12-27
@Azperin

https://learn.javascript.ru/focus-blur#vklyuchaem-...

D
Dmitry, 2020-12-28
@DKWLB

Sorry, I'm writing from a mobile phone, if it's still relevant and based on the comments above.

mplate>
  <div id="app">
    <span
      contenteditable="true"
      spellcheck="false"
      style="width: 800px; display: block"
      v-text="textEl"
      @focus="focused = true"
      @blur="focused = false"
    />
    {{ focused }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      focused: false,
    };
  },
  computed: {
    textEl() {
      if (this.focused) {
        return "text with focus";
      } else {
        return "text with not focus";
      }
    },
  },
};
</script>

Just make it a computed property and everything changes. Sorry, I couldn't save from my mobile phone on sandboxe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question