38

What is the Vue.js equivalent of the following jQuery?

$('.btn').click(function(){  $('.hideMe').hide()  });
1

5 Answers 5

47

jQuery works out of the box, Vue.js does not. To initialize Vue.js component or App you must bind that component with its data to one specific HTML tag inside your template.

In this example the specified element is <div id="app"></div> and is targeted through el: #app. This you will know from jQuery.

After you declare some variable that holds the toggle state, in this case, isHidden, the initial state is false and has to be declared inside the data object.

The rest is Vue-specific code like v-on:click="" and v-if="". For a better understanding please read the documentation of Vue.js:

Note: consider reading the whole or at least longer parts of the documentation for better understanding.

var app = new Vue({
  el: '#app',
  data: {
    isHidden: false
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <button v-on:click="isHidden = true">Hide the text below</button>
  <button v-on:click="isHidden = !isHidden">Toggle hide and show</button>
  
  <h1 v-if="!isHidden">Hide me on click event!</h1>
</div>

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

1 Comment

Just made this the answer instead of the other answer as this was the one that solved what I needed. Thanks a lot for helping!
23

This is a very basic Vue question. I suggest your read the guide, even the first page will answer your question.

However, if you still need the answer this is how you hide/show elements in Vue.

new Vue({
 el: '#app',
 data () {
   return {
     toggle: true
   }
 },
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>

<div id="app">
  <button @click='toggle = !toggle'> click here </button>
  <div v-show='toggle'>showing</div>
</div>

1 Comment

I did it in a similar way initially, but I had some syntax issues with the @click.
5
<div>
    <div>

        <button v-on:click="isHidden = !isHidden">Toggle hide and show</button>

        <h1 v-if="!isHidden">Hide me on click event!</h1>
    </div>
</div>

name: "Modal",
    data () {
        return {
            isHidden: false
        }
    }

Comments

2

The up-voted answer is definitely a way to do it, but when I was trying to do this it was with a dynamic array instead of a single Div, so a single static Vue variable wouldn't quite cut it.

As @samayo mentions, there isn't a difference between the hide action from jQuery vs Vue, so another way to do this is to trigger the jQuery through the @click function.

The Vue Dev kit will tell you not to mix JS inline with @click events and I had the same problem as @user9046370 trying to put the jQuery command inline with @click, so anyway,

Here's another way to do this:

<tr v-for="Obj1,index in Array1">
    <td >{{index}}</td>
    <td >
        <a @click="ToggleDiv('THEDiv-'+index)">Show/Hide List</a><BR>
        <div style='display:none;' :id="'THEDiv-'+index" >
            <ul><li v-for="Obj2 in Array2">{{Obj2}}</li></ul>
        </div>
    </td>
</tr>
Method:
ToggleDiv: function(txtDivID)
{
    $("#"+txtDivID).toggle(400);
},

The other perk of this is that if you want to use fancy jQuery transitions you can with this method.

Comments

-1
<template>
    <button class="btn btn-outline-secondary" type="button"><i class="fas fa-filter" @click="showFilter = !showFilter"></i></button>
</template>

<script>
    export default {
        methods:{
            showFilter() {
                eventHub.$emit('show-guest-advanced-filter');
            }
        }
    }

</script>

But it's not worked this method.

<template>
    <button class="btn btn-outline-secondary" type="button"><i class="fas fa-filter" @click="filtersMethod"></i></button>
</template>

<script>
    export default {
        data: () => ({
            filter: true,
        }),

        methods: {
            showFilter() {
                eventHub.$emit('show-guest-advanced-filter');
                this.filter = false;
            },

            hideFilter() {
                eventHub.$emit('hide-guest-advanced-filter');
                this.filter = true;
            },

            filtersMethod() {
                return this.filter ? this.showFilter() : this.hideFilter();
            }
        }
    }

</script>

This is worked.

2 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
I solved the problem, but I shared it for guidance. It would be more useful if you share the truth rather than writing it up.

Your Answer

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