Vue Js Add Class To Element On Clicked Example

Bootstrap , Bootstrap 4 , Vue.Js

Nicesnippets

59

01-08-2020


Hi Guys,

In this example, I will explain you how to add class to element on clicked in vue js, we will show example of add class to element on clicked using vue js. I will display simple add class to element on clicked in vue js.

Vue.js Script Code

Here, I will show script Code of vue js add class to element on clicked

<script>

Vue.config.devtools = true

var vm = new Vue({

el: '#app',

data: {

isAddClass: false,

},

methods: {

addClass: function() {

this.isAddClass = true;

}

}

});

</script>

Full Example

Here, I will give you full example for simply display vue js add class to element on clicked bellow.

<!DOCTYPE html>

<html>

<head>

<title>Vue Js Add Class To Element On Clicked Example</title>

<style>

.newClass

{

box-shadow: 0px 0px 5px #000;

background-color: green;

color: #fff;

padding: 3px 5px;

}

</style>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>

<body class="bg-dark">

<div class="container">

<div class="col-md-5 offset-md-3">

<div class="card mt-5">

<div class="card-header">

<h5>Vue Js Add Class To Element On Clicked</h5>

</div>

<div class="card-body">

<div class="row">

<div class="col-md-12">

<div id="app">

<button v-on:click="addClass" :class="{'newClass': isAddClass}" class="btn-block">

Click Me

</button>

</div>

</div>

</div>

</div>

</div>

</div>

</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>

<script>

Vue.config.devtools = true

var vm = new Vue({

el: '#app',

data: {

isAddClass: false,

},

methods: {

addClass: function() {

this.isAddClass = true;

}

}

});

</script>

</body>

</html>

It will help you...