Why not just use jQuery to solve this issue-
<button v-on:click="makeRed" id="mybutton">
and in your method,
makeRed: function() {
$( "#mybutton" ).addClass( ".red" );
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi guys, I have a problem. Im new to Vue and I want to apply a class to a button when the button is clicked. In jQuery I would just select the element and add the class but I dont understand how its done in vue. I have the following code:
<div id="vue-instance">
<span>{{ message }}</span>
<ol>
<li v-for="x in items">
{{ x.title }}
</li>
</ol>
<button v-on:click="makeRed" v-bind:class="'red'">
Click Me
</button>
</div>
var vm = new Vue({
el: '#vue-instance',
data: {
message: 'Hello',
items: [
{title: 'Item 1'},
{title: 'Item 2'},
{title: 'Item 3 ' + new Date() }
]
},
methods: {
makeRed: function() {
}
}
});
.red
{
border: 1px solid red;
background-color: pink;
}
Hi guys. I found a way, but I think im gonna stick to jQuery in this situation.
<div id="vue-instance">
<span>{{ message }}</span>
<ol>
<li v-for="x in items">
{{ x.title }}
</li>
</ol>
<button v-on:click="makeRed" v-bind:class="{red: isActive}">
Click Me
</button>
</div>
var vm = new Vue({
el: '#vue-instance',
data: {
isActive: false,
message: 'Hello',
items: [
{title: 'Item 1'},
{title: 'Item 2'},
{title: 'Item 3 ' + new Date() }
]
},
methods: {
makeRed: function() {
this.isActive = !this.isActive;
}
}
});
Please or to participate in this conversation.