Learn html, css, javascript online and how to make a webpage design

How to use v-model on input type checkbox and radio ?

Tuesday 26th of January 2021 05:09:09 PMv modelcheckboxradioVueJS

Today we will learn how to use the v-model in VueJS to assign and receive change data from the end user. Very easy to use, check out the example below:

v-model in input type checkbox

<div class="sub-title">Input type checkbox</div>
<ul>
    <li>
        <label>
               <input type="checkbox" v-model="receive_email" /> 
               <span class="text">Receive email notifications</span>
        </label>
    </li>
</ul>
<div class="result">Checkbox value is {{ receive_email }}</div>

 

v-model in input type radio

<div class="sub-title">Input type radio</div>
<ul>
    <li>
        <label>
            <input type="radio" v-model="gender" name="gender" value="male" /> 
            <span class="text">Male</span>
        </label>
    </li>
    <li>
        <label>
            <input type="radio" v-model="gender" name="gender" value="female" /> 
            <span class="text">Female</span>
        </label>
    </li>
</ul>
<div class="result">Radio value is {{ gender }}</div>

 

The entire source code is as below:

<!DOCTYPE html>
<html>
<head>
    <title>How to use v-model on input type checkbox and radio ? - HtmlcssDownload.com</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <style>
        body{display: flex;align-items: center;justify-content: center;min-height: 100vh;background: #433262;font-family: Arial, Helvetica, sans-serif;}
        .box{width: 500px;padding:20px;background: #fff;border-radius: 20px;}
        .box .title{text-align: center;font-size: 19px;font-weight: bold;margin-bottom: 20px;}
        .box .sub-title{font-size: 17px;font-weight: bold;margin-top: 30px;}
        .box ul{margin:0px;padding:0px;}
        .box ul li{display: flex;align-items: center;width: 100%;height: 40px;;}
        .box ul li .text{margin-left:10px;}
        .box ul li label{cursor: pointer;}
        .box .result{height:40px;display:flex;align-items:center;}
    </style>
</head>
<body>
<div id="app">
    <div class="box">
        <div class="title">Use v-model on input type checkbox and radio </div>
        <div class="checkbox">
            <div class="sub-title">Input type checkbox</div>
            <ul>
                <li>
                    <label>
                        <input type="checkbox" v-model="receive_email" /> 
                        <span class="text">Receive email notifications</span>
                    </label>
                </li>
            </ul>
            <div class="result">Checkbox value is {{ receive_email }}</div>

            <div class="sub-title">Input type radio</div>
            <ul>
                <li>
                    <label>
                        <input type="radio" v-model="gender" name="gender" value="male" /> 
                        <span class="text">Male</span>
                    </label>
                </li>
                <li>
                    <label>
                        <input type="radio" v-model="gender" name="gender" value="female" /> 
                        <span class="text">Female</span>
                    </label>
                </li>
            </ul>
            <div class="result">Radio value is {{ gender }}</div>
        </div>
    </div>
</div>
   
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            receive_email: false,
            gender: "male"
        }
    })
    
</script>
    
</body>
</html>

 

Result for example

 

We are Recommending you: