Display or conceal a div element depending on the value selected in Vue.js

I have a Vue.js dropdown and I would like to show or hide my div based on the selected value in the dropdown. The current code is only working for one ID, but I want it to work for all IDs. I want to toggle the visibility of my div based on the option's value. For example, if the value is 1, then only the test_1 div should be shown.

<template>
  <div>
    <multiselect
      v-model="optionsData[0]"
      :options="optionsData"
      :custom-label="nameWithLang"
      placeholder="Pick some"
      label="data"
      track-by="data" 
      @change="hideshowdiv"/>
  </div>
  <div id="test_1">test 1</div>
  <div id="test_1">test 1</div>
  <div id="test_1">test 1</div>
  <div id="test_1">test 1</div>
  <div id="test_1">test 1</div>
  <div id="test_2">test 2</div>
  <div id="test_2">test 2</div>
  <div id="test_2">test 2</div>
  <div id="test_2">test 2</div>
  <div id="test_2">test 2</div>
</template>

export default {
  components: {
    Multiselect,
    InputTag
  },
  data() {
    return {
      optionsData: [
        { id: '1', data: 'Value1' },
        { id: '2', data: 'Value2' }
      ],
    };
  },
  methods: {
    hideshowdiv(value) {
      document.getElementById("test_" + value).style.display = "block";
    }
  }
}

Answer №1

consider using v-if

make changes to your HTML as shown below with the use of v-if

<div id="test_1" v-if="selectedValue == 1">test 1</div>
  <div id="test_1" v-if="selectedValue == 1">test 1</div>
  <div id="test_1" v-if="selectedValue == 1">test 1</div>
  <div id="test_1" v-if="selectedValue == 1">test 1</div>
  <div id="test_1" v-if="selectedValue == 1">test 1</div>
  <div id="test_2" v-if="selectedValue == 2">test 2</div>
  <div id="test_2" v-if="selectedValue == 2">test 2</div>
  <div id="test_2" v-if="selectedValue == 2">test 2</div>
  <div id="test_2" v-if="selectedValue == 2">test 2</div>
  <div id="test_2" v-if="selectedValue == 2">test 2</div>

next, define the selectedValue variable, you can set a default value for it too

data: () => ({
    optionsData: [
      { id: '1', data: 'Value1' },
      { id: '2', data: 'Value2' }
    ],
    selectedValue: Number
  })

update the method to assign a value to selectedValue

method() {
hideshowdiv(value) {
  this.selectedValue = value;
}}

Answer №2

Prior to setting your div to "display: block", ensure all other divs are set to "display:none". The display property does not automatically change from "display:none".

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

Dynamic blog posts experiencing issues with syntax highlighting feature

I am currently developing a blog using Vue and have decided to incorporate syntax highlighting for the code snippets in my posts by utilizing vue-highlightjs. In order to write the content of my blog posts, I am simply using a textarea within my admin pane ...

How to insert a span element within a link tag in Next.js/React.js

Looking to create a breadcrumb using microdata in a Next.js and React.js project. Initially, I tried the following: <li itemProp="itemListElement" itemScope itemType="https://schema.org/ListItem"> <Link href={'/index&a ...

Struggling to locate a straightforward sidebar solution without relying on Bootstrap. Finding an easy-to-use answer seems

I have a lightweight and easy-to-understand code for a website project with two panels. The first panel on the left is a large navigation (270px width, top to bottom, similar to a wiki) with approximately 30 unordered list items. Next to it is the second ...

Tips for sending a variable from Javascript to node.js, specifically connecting to MYSQL

Can you help me with a simple example on how to pass a variable from JavaScript to Node.js? I need to store the user input from a text box in Node.js and perform some actions. Client <!DOCTYPE html> <html> <body> <h2>HTML Forms< ...

Converting a string to a number, even if it contains non-numeric

Is there a built-in function that can directly convert a string containing non-numeric characters to a number in JavaScript, without the need for using str.substring() followed by parseInt()? For instance, how can I efficiently convert the string x1 to th ...

PWA JavaScript struggling with GPS geolocation coordinates

I am experiencing issues with the GPS coordinates being stuck when using geolocation in a Progressive Web App (PWA). Sometimes, the coordinates get stuck at the previous location, even if the user has moved since then. I suspect that this is due to the GP ...

Journeying through JSON: Presenting the value alongside its hierarchical parent

I'm completely new to JSON Path, so I'm not sure how complicated this could be, or if it's even possible. The JSON structure I have consists of multiple groups, each containing a set of fields. Both the groups and the fields have their own ...

Tips for creating a worldwide shop Object?

Working with vueify and browserify, I have created an object in my main.js file var store = { foo: 'bar' }; var vm = new Vue({ el: '#app', data(){ return { foo: store }; }, components: { Login, Registration, ...

Determine the Number of Table Columns Using jQuery

I'm curious, with jQuery how can one determine the number of columns in a table? <script> alert($('table').columnCount()); </script> <table> <tr> <td>spans one column</td> <td ...

JavaScript watermark with alternating/dual mode

In the following code snippet, I have implemented a function to make window.status switch between displaying "a" and "b." function alternateViaIntrvl() { setInterval('alterStatus()', 500); } var altrCounter = 0; function alerted() { var ...

There is no throttleTime function available in Angular 6 within Rx Js

Currently, my Angular 6 project is utilizing angular/cli": "~6.1.5 and rxjs": "^6.0.0. As a newcomer to Angular 6, I decided to dive into the official documentation to enhance my understanding. Here's a reference link I found useful: http://reactivex ...

Swap out the svg element with an icon alternative

Transforming Circle Elements into Shopping Cart Icons I am attempting to change the appearance of my SVG circle elements to resemble shopping carts. Is there a method to completely alter the definition of a circle element in svg so that it displays a spec ...

Simplified method of connecting dynamic components without needing to remember functions in jQuery

I have a page where users can freely move, resize, and modify content. The basic setup is as follows: $('.element').on().resizable().draggable(); When this code is called on page load, it allows elements to be resizable and draggable. The issu ...

Progress bar carousel component based on advancement movements

Here is the mockup I am working with: https://i.sstatic.net/bIepQ.png So far, I have made good progress with this code: https://codesandbox.io/s/codepen-with-react-forked-16t6rv?file=/src/components/TrendingNFTs.js However, I am facing a couple of challe ...

The URIError occurred while attempting to decode the parameter '/December%2015,%' within the express framework

After setting up a middleware using the express framework that handles a URI with a date as a parameter, I encountered a small issue. app.get("/:date",function(req,res){ var result; var myDate=req.params.date if(moment(myDate).isValid()) ...

When importing another JavaScript file, the export default async function may not be fully executed

I am facing an issue while trying to define the axios baseURL by triggering an internal API call in http-common.js. When I import the module from http-common.js, it fails to obtain the axios object. Even after running the async function, it still doesn&apo ...

Launching a segment from a different page within a foundation reveal modal

With the goal of displaying a modal that contains content from a section on another page when a link is clicked, I encountered a specific issue. For instance: <a href="/otherpage" data-reveal-id="myModal" data-reveal-ajax="true"> Click Me For A Mod ...

In a Django template, implement a checkbox feature in the list view to select multiple objects. Retrieve all selected checkbox objects for each pagination and display them in

In my HTML template, I have a list view with checkboxes and pagination. My goal is to retrieve all the checked box objects from each page's pagination and send them to the server (specifically the view part of Django). For example, if I check 4 object ...

Tips for shrinking the size of electron applications

After installing my electron application on Mac, I noticed that it is surprisingly large at 1.39GB, while the Windows version is only around 70MB. Upon unpacking the dmg file, I discovered a file named app.asar which accounts for a significant portion of t ...

Button disabled is not functioning properly

Does anyone know why my button is not being disabled when I am not typing in the textbox? Here is the code snippet: $(document).ready(function () { loadData(); function loadData(is_category) { $(document).on('click&apo ...