Vue JS - Toggle visibility of an element based on radio button selection

When selecting between true or false radio buttons, I want a block to appear when "One" is clicked and be hidden when "No" is clicked. Here is the code snippet I used:

<template>
  <div>
    <div>
      <label
        >One
        <input type="radio" name="radio" value="true" v-model="websiteAccept" />
      </label>
      <label
        >No
        <input
          type="radio"
          name="radio"
          value="false"
          v-model="websiteAccept"
        />
      </label>
      <p>{{ websiteAccept }}</p>
    </div>

    <div v-if="websiteAccept" style="background: yellow">
      <p>Hide / Show block</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      websiteAccept: null,
    };
  },
};
</script>

Although this method does not seem to work as expected, it could be due to the dynamic change of the websiteAccept property upon clicking the radio buttons.

You can view the code on codesandbox

Answer №1

To ensure your radio button values are treated as booleans instead of strings, make sure to bind them properly:

   <div>
      <label
        >Yes
        <input type="radio" name="radio" :value="true" v-model="websiteAcceptance" />
      </label>
      <label
        >No
        <input
          type="radio"
          name="radio"
          :value="false"
          v-model="websiteAcceptance"
        />
      </label>
      <p>{{ websiteAcceptance }}</p>
    </div>

Answer №2

<template>
 <div>
   <div>
     <label
       >Yes
       <input type="radio" name="radio" :value="true" v-model="websiteAccept" />
     </label>
     <label
       >No
       <input
         type="radio"
         name="radio"
         :value="false"
         v-model="websiteAccept"
         @change="change"
       />
     </label>
     <p>{{ websiteAccept }}</p>
   </div>

   <div v-if="websiteAccept" style="background: yellow">
     <p>Toggle Block Visibility</p>
   </div>
 </div>
</template>

<script>
export default {
 data() {
   return {
     websiteAccept: null,
   };
 },
};
</script>

Change the :value to set the value as BOOLEAN instead of your code which sets it as a string.

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

What's the best way to conduct a snapshot test on a Vue Single File Component page within a Nuxt application that solely consists of a

Is it possible to perform a snapshot test on a Vue SFC page in Nuxt that only has a layout defined, using jest? For instance: <script> export default { layout: 'some-layout-name' }; </script> When attempting to generate a snapshot ...

Executing a function when a webpage loads in Javascript

I have created a responsive website using Twitter Bootstrap. On my site, I have a video displayed in a modal that automatically plays when a button is clicked. Instead of triggering the function with a button click, I want the function to be called when ...

Event fails to emit when used in conjunction with router-link

I created a basic Vue application: const Home = { template: '<div>Home</div>' } const Bar = { methods: { bar () { alert('bar') this.$emit('test') } }, template: ` <div> ...

The Twitter timeline exceeded its rate limit on YQL. What can be done as

I am seeking advice for the following situation: I am interested in using YQL to retrieve Twitter timeline feeds. Example of feed: Here is the YQL query string I am using: select * from json where url="https://api.twitter.com/1/statuses/user_timeline.j ...

When using the setTimeout function to update the state, React useContext appears to be ineffective

As a newcomer to React, I have a question about refreshing the score in my card game within a forEach loop using setTimeout. While the state appears to update correctly, the DOM (Component overarching) does not reflect these changes. export function Refill ...

Modify the background color of a specific bar across multiple charts when hovering or clicking - utilizing chart.js

I have multiple chart.js canvas elements on a webpage. The goal is to be able to quickly identify and highlight the same bar value across all the charts. For example, when I hover over a bar called "Thu" on the first chart, I want to automatically search f ...

worldpay implements the useTemplateForm callback function

My experience with implementing worldpay on my one-page Angular app (Angular 1.x) has been mostly positive. I have been using the useTemplateForm() method to generate a credit card form and retrieve a token successfully. However, I have encountered an issu ...

Embedding a YouTube video in a view player using HTML5

So I've got a question: can you actually open a youtube video using an HTML5 video player? I'm looking for a more mobile-friendly way to watch youtube videos, and my idea was to upload a thumbnail image and then set up an onclick function to disp ...

Utilizing webpack for server-side development

I'm currently in the process of creating a node back-end service using express and I was thinking about incorporating webpack to bundle everything into a single file (although I'm not sure if this approach is correct as I'm still learning). ...

Exploring the possibilities of toggling between a personalized CSS design and a Bootstrap CSS layout

Is it possible to implement a dropdown menu on my sample-page using javascript/jquery in order to switch between a custom layout and a bootstrap layout? ...

Using Jquery to create interactive and dynamic webpage elements

I am struggling with a paragraph containing words in a span that are editable upon clicking. The content needs to be dynamically called, but my current approach is not effective. Can anyone provide a solution or a better way to achieve this? Feel free to ...

The challenge with the mousewheel function in THREE.js Editor

Attempting to create a basic scene in the THREE.js Editor. Using the built-in Script editor, all control functions seem to be functioning correctly except for the mousewheel (I've tried mousedown, mousemove, etc.). I even attempted to add a listener ...

Make sure that the parent element is only visible once all of its child elements have

As a newcomer to the world of react, I am facing some challenges. I have created a form in react which includes a dropdown. To ensure reusability across multiple pages, I decided to turn this dropdown into a component that is responsible for fetching all n ...

The art of linking JavaScript events

I'm encountering some challenges with linking events together. I've set up an eventListener on a variety of links that, when hovered over, trigger a drop-down menu to appear. Everything is functioning properly, but I also want triangular shapes ...

``When executing the `npm install` command, it does not install the sub-dependencies of a local package

I am facing an issue with my packages. One package named package-a has a dependency on another package called package-b which is not published on npm but resides in my file system. When I try to run npm install from the directory of package-a, the dependen ...

Executing a javascript function from an ajax-loaded webpage

I have a form where I upload a file using an ajax call that includes an API request. Once the API call is successful, I need to update a table displaying the list of uploaded files. Initially, I attempted calling a JavaScript function within the ajax page, ...

What is the process for rendering a page in node express from a route.post method?

My webpage fetches 100 memes from an API and displays them in a table. Each meme has a details button that should take the user to a specific page for that meme. However, even though my POST request to the meme route is successful, the meme details page is ...

What is the best way to show both the input and output on one page?

I have a single PHP file with two separate PHP tags. When I click on the first row in the table of the first PHP tag, I want to display the value of the first row in the table of the second PHP tag on the same page. The common field for both tables is or ...

``motioning a component beneath another component that may be in a state

Having an issue with my CSS. I have some elements generated by JavaScript and when hovering over them, another element is displayed below the others for some reason. Here's the CSS related to this problem: .hiddenTextjob { display:none; ...

Having trouble viewing the image slider on an HTML webpage

I've been attempting to incorporate an image slider into my personal website on GitHub, but I've encountered some difficulties as the images are not displaying. Interestingly, I can load individual images without using the slider and they show up ...