1

I would like to transform the following code which is in javascript:

$('a').click(function() {
    $(this).find('i').toggleClass('fa-heartbeat');
});

in vue.js.

function name: like

javascript test: https://jsfiddle.net/jsk590ep/

3
  • 2
    Please, edit your question to tell the community what you have tried so far. What's your problem on converting this code. Commented Aug 7, 2019 at 23:33
  • So I try to convert the code, but it won't. Inside my method: {} I created: like: function () {find ('i'). ToggleClass ('fa-heartbeat')}, but I don't look for the function, and I need an ID in that function called: like Commented Aug 7, 2019 at 23:40
  • 2
    You can click on the 'Edit' button below your question to add that Commented Aug 7, 2019 at 23:41

1 Answer 1

4

In Vue, you typically don't select and manipulate DOM elements directly, you rather bind data to parts of the markup within your Vue components.

That said: You don't even need a function for that. Simply

 <a href="#" @click="liked = !liked">
   <i :class="['fa', liked ? 'fa-heartbeat' : 'fa-plus-circle']"></i>
 </a>

When looking at the vue docs, note that @click in the example is a shortcut for v-on:click and :class for v-bind:class.

Working example here: https://codesandbox.io/s/stack-overflow-q-57403395-ul62e?module=/src/App.vue

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.