Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide div onclick in Vue.js

People also ask

How do I add TS to Vue?

To let Typescript enters into your Vue components, 2 additions are mandatory in each component file: Adding the lang="ts" attribute to the script tag so that Typescript can recognized it.

How do I hide my Vue template?

Make one div v-cloak--inline which we want to show before HTML page rendered. Another Div which will contain the full page with v-cloak--hidden . Must have to add the css's given here.

Is it good to use TypeScript with Vue?

Another benefit or working with TypeScript for your Vue. js projects, is that you can refactor with more confidence. Let's say you had a PostForm component for a blog application that looked something like this in plain JavaScript. It keeps track of a title and a body as reactive references.

How do I hide a div in Vue?

Conclusion. Vue gives you a bunch of good ways to hide the element on the screen. When using v-if="false" the element isn't rendered at all in the DOM. When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely.


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 been 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 better understand please read the documentation of Vue.js:

  • The Vue Instance
  • Template Syntax
  • Event Handling
  • Conditionals

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>

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>

<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
        }
    }

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.