Learning how to hide a specific div in Vue.js when another div is clicked

<template>
 <div v-for="(item ,index) in items" v-if="showing">
                <span @click="showing=false">{{item.name}}</span>
 </div>
</template>
    <script>

            export default{
                       showing:true,
                       items: [
                          {'name':'a'},
                          {'name':'b'},
                          {'name':'c'},
                       ],
            }
    </script>

How can I hide a specific div when clicking on it? **Whenever I try this method, all of them are hidden at once.**

Answer №1

This is my approach:

<template>
   <div v-for="(item,index) in items" v-if="hiddenItems.indexOf(index) < 0">
     <span @click="hiddenItems.push(index)">{{item.name}}</span>
   </div>
</template>
<script>
  export default{
    hiddenItems:[],
    items:[
     {'name':'apple'},
     {'name':'banana'},
     {'name':'carrot'}
    ]
  }
</script>

You can view the implementation here.

Answer №2

It's been a while since I worked with Vue, so this code might need some tweaking, but it should give you a general idea.

<template>
    <div v-for="(item, index) in items" v-if="item.shown">
         <span @click="item.shown=false">{{item.name}}</span>
    </div>
</template>
<script>
        export default {
             items: [
                 {'name':'a', shown: true},
                 {'name':'b', shown: true},
                 {'name':'c', shown: true},
             ],
        }
</script>

Answer №3

To avoid using another array to store hidden indexes (like hide :[]):

new Vue({
    el: "#app",
      data: function() {
    return {
      items:[ {'name':'a'}, {'name':'b'}, {'name':'c'} ]
    }
  },
  methods: {
    setHide: function(item) {
      this.$set(item, 'hide', true);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.5/vue.js"></script>

<div id="app">
  <div v-for="item in items" :key="item" v-if="!item.hide">
    <span @click="setHide(item)">{{item.name}}</span>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The impact of increasing memory usage in Chrome by modifying global variables

Recently, I've been delving into optimizing JavaScript code for creating HTML5 games, particularly with a focus on mobile browsers. My journey began with comparing different engines and gradually simplifying the code to discover something that has lef ...

MongoDB: total value of combined fields

Our records show: {a: 1} {a: 5} {a: 3} Is there a way to aggregate this data and calculate the current sum? {a: 1, cs: 1} {a: 5, cs: 6} {a: 3, cs: 9} ...

Methods for handling asynchronous completion using jQuery

While there are numerous discussions on this topic, I have not found a definitive answer to my question. The solutions proposed often seem overly complex for what should be a simple issue. So, my main question is how can I make the following code run in s ...

What is the purpose of encoding the Jquery Ajax URL twice?

In my JavaScript code, I am utilizing the jQuery ajax function. Here is an example of how it appears: $.ajax({ url: "somepage/" + keyword + "/" + xyz + "/" + abc + "/getbla", (...) If the 'keyword' (value from a textfield) includes a ' ...

Issue with Loading.gif not displaying properly in Lightbox 2 after opening each image

In the website I am currently working on, I have integrated Lightbox2 to display pop-up images. However, I notice that the loading gif only shows up for the first image and then disappears for subsequent ones. Is there a way to modify the settings so tha ...

When utilizing the `express.static(__dirname)` function in Node.js with Express, the visitor may be directed to an incorrect HTML page

My website consists of a login page named login.html and an index page named index.html. I want to implement authentication so that only logged in users can access the index page. I have not set up the post method on the login HTML page. Therefore, I have ...

Utilizing JavaScript Event Listener to Identify the Relevant PHP File for Display

I have incorporated two separate PHP files in my code: tabs.php accordion.php Both files iterate through the same dataset, but present the information in different ways. The choice between displaying tabs or accordions depends on the user's screen s ...

Triggering OnClick() more than once may result in the function failing to execute as intended

My dilemma involves a table of cells where clicking a cell triggers an event. I need to dynamically add cells, so I plan to call OnClick again on all rows. But when I do so for the second time, cells that have already had OnClick called twice stop firing a ...

obtain an inner element within a container using the class name in a div

I am attempting to locate a span element with the class of main-tag within a nested div. However, I want to avoid using querySelector due to multiple elements in the HTML file sharing the same class and my preference against using IDs. I realize there mig ...

What is the best way to synchronize the image dimensions and source when dynamically loading images?

I am facing an issue with a function that updates images by altering the src and css (width/height) of an IMG tag within the document. Below is a simplified example to demonstrate the problem: updateImage = function(src, width, height) { $("#changeMe"). ...

Vue Error: The method "reduce" is not a function

Currently implementing Vue.js with Typescript and aiming to utilize reduce for summing up the values of desktopCnt and mobileCnt from the deviceCount array to display their total numbers. The deviceCount array structure is as follows: [ { " ...

Is it possible to use the skip function with GroupBy in SQL?

I have a query below along with some additional description of what I am looking for. $recommendedByVideoId = RecommendedVideo::selectRaw('video_id,id') ->orderBy('played_count', 'desc') -&g ...

Encountering issues with implementing Bootstrap modal in Laravel project

Whenever I try to click on the edit post dropdown link, the page just refreshes instead of opening a modal as expected. Here are the included scripts in my layout view: <script src="https://code.jquery.com/jquery-1.12.0.min.js" ></script> & ...

Styling sub-table titles in jQuery jTable with proper indentation

I am currently implementing jquery.jtable, where I have rows that invoke the openChildTable method in the jquery.jtable.js file. I'm attempting to figure out a way to indent the heading of the child table. Below is the code snippet responsible for cr ...

Deselect the checkbox that was initially selected when an alternative checkbox option has been chosen

I am trying to implement a feature where there are 2 groups of checkbox options on the same page. Below is the code snippet: <div class="col-xs-12"> <label class="checkbox-inline"> <input type="checkbox" th:field="*{borrowerRace1}" th:val ...

Leveraging AngularJS $promise in conjunction with NgResource

As I delve into the world of AngularJS, I have encountered promises which have proven to be quite beneficial in my learning journey so far. Now, I am eager to explore the optional library resource.js in AngularJS. However, I stumbled upon examples that lef ...

What is the most efficient way to enable seamless communication between sibling components in Vue.js 2?

Just starting out with Vue.js 2 and I'm really enjoying it so far despite being new to it. Currently using the latest version. I've begun working on my first larger application and ran into a scenario right off the bat where I have a main app co ...

Mark all checkboxes as checked automatically

Can anyone help me with my checkbox group issue? b-form-checkbox-group.ml-4.b-check( v-model="selected", v-if="productGroup.show", :options="productGroup.products", I want the list o ...

Calculating the total sum of values within a JSON array

Here is the data that I am working with: input1 = [{ "201609": 5, "201610": 7, "201611": 9, "201612": 10, "FY17": 24, "metric": "metric1", "careerLevelGroups": [{ "201609": 3, "201610": 6, "201611": ...

The optimal time to register for events within the Vue lifecycle

Currently, I am developing a Vue2 component using vue-component that includes a subcomponent. Here is an example: <note :bus="bus" :itemId="selectedId"></note> The subcomponent contains the following code snippet: <textarea v-model="text" ...