Is it possible to set the value of a radio button in vue.js by simply clicking on its label?

Is it possible to use the label of a radio input to trigger the output of its value?

I am currently utilizing v-model in vue.js and have hidden my radio buttons (

input[type="radio"]{display: none;
). I am attempting to capture the value of the radio buttons when clicking on their corresponding labels. However, this approach is not functioning as expected.

JSFiddle Link for reference: https://jsfiddle.net/9t41L6my/3/

Answer №1

Ensure that all radio buttons share the same name attribute to function as a cohesive group. Additionally, make sure the <label>.for property matches the corresponding <input> element's id, enabling users to select options by clicking on the labels.

Your HTML template structure should resemble this:

<div class="timeline__radio">
  <input id="daily" class="timeline__input" type="radio" name="timeline" value="daily" v-model="checked">
  <label for="daily" class="timeline__label">Daily</label>
</div>
<div class="timeline__radio">
  <input id="weekly" class="timeline__input" type="radio" name="timeline" value="weekly" v-model="checked">
  <label for="weekly" class="timeline__label">Weekly</label>
</div>
<div class="timeline__radio">
  <input id="monthly" class="timeline__input" type="radio" name="timeline" value="monthly" v-model="checked">
  <label for="monthly" class="timeline__label">Monthly</label>
</div>
<div class="timeline__radio">
  <input id="yearly" class="timeline__input" type="radio" name="timeline" value="yearly" v-model="checked">
  <label for="yearly" class="timeline__label">Yearly</label>
</div>

To view a demonstration, visit this link.

Answer №2

Ensure consistency in input names

name="same"

Assign the labels' `for` value as the inputs' IDs

<input id="daily" value="daily" v-model="checked" />
<label for="daily">Daily Label</label>

Remove the @click="checked" directive

Explicitly set the values without binding

value="daily"  // NOT :value="daily"

View an illustrative example

<input id="daily" type="radio" name="same" value="daily" v-model="checked">
<label for="daily">Daily</label>

<input id="weekly" type="radio" name="same" value="weekly" v-model="checked">
<label for="weekly">Weekly</label>

<input id="monthly" type="radio" name="same" value="monthly" v-model="checked">
<label for="monthly">Monthly</label>

<input id="yearly" type="radio" name="same" value="yearly" v-model="checked">
<label for="yearly">Yearly</label>

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 is the method for a mono social icon to function without requiring a specific class for each icon

Curious about the magic behind Mono Social Icons and how it effortlessly displays the correct icon. Unlike other font icon sets that require adding a class name to load a utf-8 character in :after, Mono Social Icons keeps it simple. Just add the icon as t ...

Vue zooming element

Struggling to figure out how to implement this plugin for zooming and panning multiple components. I am interested in using the https://github.com/timmywil/panzoom library as it seems like a good option. Just started playing around with it and my initial ...

Organizing my website directories using external tools

I am dealing with a website that follows this specific structure: \ |--css | \ | |--vendors | | \ | | |--normalize.css | | |--... | | | |--styles.css | |--media-queries.css | |--js | \ | |--vendors | | \ | | |- ...

Enhancing Blog Navigation with PicoCMS Pagination

After successfully setting up PicoCMS on my website, everything seems to be working well except for the blog pages. I am having issues with the "Older Posts" button not functioning as expected. Unfortunately, I couldn't find any information in the the ...

Modify CSS to switch body background image when a DIV is hovered

Feeling a bit lost on where to start with this issue. My divs are positioned as desired on the page. Just to clarify, I am utilizing CSS Grids for this. In short, all I'm wondering is if it's possible to change the background of the body elemen ...

Display only the static placeholder in Angular 2 multi-select feature

My experience with angular 4 is fairly new and I've recently delved into using the angular multiselect feature with the npm package available here. I've managed to successfully configure it, as well as capture selected and deselected items/event ...

Automatically hiding divs when clicked using HTML and CSS

Apologies if this question has been asked before, but I have two onclick divs and I'm trying to achieve a specific behavior. When I open the first one, I want it to automatically close when I open the second one, and vice versa. Below is the HTML cod ...

Working condition may vary depending on the size of the item

I have integrated the Masonry jQuery plugin into my design, but it is not functioning properly. The CSS class mentioned in the documentation only includes: width: 25% For my purposes, I modified it to: width: 24% float: left margin-right: 5px This modi ...

In Safari, use a reversed angle for a -webkit-linear-gradient

While searching for a CSS solution to create a uniformly colored element with an angled start without using images, I came across a simple example that caught my attention: . This method works well in most browsers, but I encountered an issue in Safari whe ...

Efficiently centering content in a grid layout using automatic fit repetition for optimized responsiveness

I've implemented a responsive grid where each item has its own hidden details section that is revealed upon clicking the item. The structure of the HTML/CSS setup is as follows: <div class="grid"> <div class="item"> ...

Vue.js framework is experiencing an issue where button click events are not firing as expected

I am working with two files here. One is an HTML file that contains the button definition, and the other is a JavaScript file that implements the button event using the Vue.js framework. The concept behind this example is that when the button is clicked, a ...

What is the best way to display line breaks that are included within a string?

My strings with line breaks are currently rendering as <br> instead of actual line breaks. I attempted to use back-ticks but that did not work. Is there an easy way to display line breaks on the page without making significant changes to my code? H ...

Div scrolling allows for parallel viewing of PDFs in a side by side arrangement

I have two scrollable elements on my webpage: one positioned on the left side and the other on the right side. The left element is a regular div, while the right element is an object containing an embedded PDF. <object width="100%" height=&quo ...

Utilizing JQuery to modify CSS in a partial view

My layout includes a button and a dynamically filled div below it using ajax and a partial view. Here is the code related to the button: $(document).on('click', '.btn-release', function () { StartReleaseVersionEdit(); ReleaseV ...

Create a situation where a span intersects with an "overflow" element

I have come across an issue with my webpage. I am utilizing a menu of icons on the left side, and it shows a span when I hover over an icon. Since my list is quite long, I want it to display a scrollbar if the window size is smaller: overflow-y: auto; ove ...

Exclude the CSS file from all elements except for the ones that are being appended to a specific div

Imagine you have a document with a CSS file that applies to all elements on the page. Is there a way to selectively remove or add styles from this file so they only affect a specific div and not the entire document? ...

How can I apply a CSS class to a group of radio buttons within a radio button list?

I'm attempting to validate a radio button group and apply a specific CSS class to it. Since this is a server control, I can only apply the class one way. However, upon checking with jQuery, I discovered that the class is actually being attached to the ...

Steps for inserting a new entry at the start of a dictionary

My fetch method retrieves recordings from the database, but I need to prepend a new record to the existing data for frontend purposes. Can someone assist me with this? <script> export default { components: { }, data: function() { ...

Centering an image vertically in a fluid navigation bar

I'm currently working on creating a fluid navigation bar in Dreamweaver that adjusts the size of the logo when changing the viewport. Everything looks great, except for one issue - the logo image doesn't stay vertically centered when it gets smal ...

What could be causing the image file input to appear sideways or flipped?

I am currently working on a Vuejs component that allows users to select a file from their local system. Once the user selects an image, it is previewed in a div. However, I have noticed that some images appear 'flipped' sideways automatically, es ...