How can the color of a button be changed when a checkbox is clicked?

Is there a way to modify the color of a button when clicked? Once the user has filled in all the fields and checked the checkbox, the button's color should change.

new Vue({
  el: '#app',
  data() {
    return {
      terms: false,
      fullname:'',
       maxfullname: 10,
      mobile: '',
      maxmobile: 10,
      area: '',
      maxarea: 12,
      city: '',
      maxcity: 12,
    };
  },
  computed: {
    isDisabled: function(){
       return !this.terms || (this.fullname.length  < this.max) || (this.mobile.length < this.maxmobile)
   || (this.area.length < this.maxarea) || (this.city.length < this.maxcity);
    }
  }
})
.register-button {
  width: 160px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background-color: #ee1d24;
  border-radius: 10px;
  margin-top: 15px;
  padding: 0 20px;
  cursor: pointer;
  opacity: 0.5; 
  display: flex;
  justify-content: center;
  align-items: center;
  outline: none;
  border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p>
    <label for='terms'>
      <input id='terms' type='checkbox' v-model='terms'/> I accept terms!!!
      <input id="fullname" type='text' v-model='fullname'  :maxlength="maxfullname"/> name
      <input id="mobile" type='text' v-model='mobile'/ :maxlength="maxmobile"> mobile
       <input id="area" type='text' v-model='area' :maxlength="maxarea"/> area
      <input id="city" type='text' v-model='city':maxlength="maxcity"/> city
    </label>
    
  </p>
  <button  class="register-button" :disabled='isDisabled'>Send Form</button>
</div>

Is there a way to modify the color of a button when clicked? Once the user has filled in all the fields and checked the checkbox, the button's color should change.

Answer №1

You can implement class binding functionality in Vue using the following code:

:class="{'active': isEnabled}"

new Vue({
  el: '#app',
  data() {
    return {
      isLoggedIn: false,
      username:'',
      maxUsernameLength: 10,
      password: '',
      maxPasswordLength: 8,
      email: '',
      maxEmailLength: 15
    };
  },
  computed: {
    isEnabled: function(){
        return this.isLoggedIn && this.username.length < this.maxUsernameLength && this.password.length < this.maxPasswordLength && this.email.length < this.maxEmailLength;
    }
  }
 })
.submit-button {
  width: 120px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
  color: #fff;
  background-color: #3498db;
  border-radius: 8px;
  margin-top: 10px;
  padding: 0 15px;
  cursor: pointer;
  opacity: 0.6; 
  display: flex;
  justify-content: center;
  align-items: center;
  outline: none;
  border: none;
}

.active {
  background-color: #27ae60;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p>
    <label for='isLoggedIn'>
      <input id='isLoggedIn' type='checkbox' v-model='isLoggedIn'/> Log me in
      <input id="username" type='text' v-model='username'  :maxlength="maxUsernameLength"/> username
      <input id="password" type='password' v-model='password' :maxlength="maxPasswordLength"> password
      <input id="email" type='email' v-model='email' :maxlength="maxEmailLength"/> email
    </label>
    
  </p>
  <button class="submit-button" :class="{'active': isEnabled}" :disabled='!isEnabled'>Submit Form</button>
</div>

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

"Styling a cover image in CSS without the need for readjustments

I found a cool idea for creating a Facebook-like cover image on this community. Check out this jsfiddle that I stumbled upon. .cover { width: 100%; height: 180px; overflow: hidden; background-color: black; } .cover > img { position: relative; width: 10 ...

Consistent Errors with AJAX POST Requests Despite CORS Enablement

Here is a function I have created for making an ajax post request: function POST(url, data) { $.ajax({ 'type' : "POST", 'url' : url, 'data' : data, headers : { 'Access-Cont ...

What could be causing my Apollo useLazyQuery to be triggered unexpectedly within a React hook?

import { useLazyQuery } from '@apollo/client'; import { useEffect, useState } from 'react'; import { ContestSessionResponseInfoObject, GetSessionDocument, HasAccessToRoundDocument, } from '@/graphql/generated/shikho-private- ...

PHP variable not receiving Ajax variable

As a beginner in Ajax and jQuery, I am learning through Stack Overflow and online tutorials. Please be considerate of my novice level as you read and potentially offer advice on my post. I have successfully created a form that displays a message upon subm ...

In the template, I utilize both vue-router and jQuery. However, there seems to be an issue with $(document).ready() not functioning

Looking for guidance on using jQuery in Vue. I've noticed that when I refresh the page, $(document).ready() function is triggered. However, when I use vue-router to switch pages, $(document).ready() does not seem to work. <template> <div ...

How to achieve an endless cycle using Promise recursion in a NodeJS environment

I am planning to replace my blocking infinite while loop with promises. My run function is quite simple - it lights up an LED and then turns it off before moving on to the next one. Since Promises do not work inside while loops, I'm exploring how I c ...

Using Django ajax doesn't function properly when it's in a separate file

My Django site utilizes AJAX to handle requests. Initially, I had the JavaScript code embedded within the HTML document using <script>...</script>, which worked perfectly fine. However, when I decided to move the JavaScript to a separate file, ...

React Warning: Every child component within a list must contain a distinct key property

Can you spot the issue in the following code snippet: <List> {sections.map(section => ( <> {section.header && <ListSubheader key={section.header}>{section.header}</ListSubheader>} {section.items ...

Pause for a moment before commencing a fresh loop in the FOR loop in JavaScript

Behold, I present to you what I have: CODE In a moment of curiosity, I embarked on creating a script that rearranges numbers in an array in every conceivable way. The initial method I am working with is the "Selection mode", where the lowest value in th ...

What is the functionality of {all: initial} and how does it address issues with default values?

Why do paragraphs inherit properties like line-height and text-align from the div element? If values for these properties are: normal -> for line-height [i.e. font-size = 16px (initial / default value) * normal (value is usually around 1.2 -> 19.2 ...

Obtain the class attribute of a CSS element with whitespaces using PHP's XML/HTML

I'm currently facing an issue with the PHP XML DOM parser. When parsing HTML from the real world, I noticed that many elements have multiple CSS classes due to spaces in the 'class' attribute. However, when using getAttribute() on a DOMNode, ...

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

Trouble confirming the password field with regular expressions in angular.js

I'm trying to validate my password field with specific special characters requirements. The field must contain at least one number, upper case letter, lower case letter, and an underscore, all of which are mandatory. I have attempted to achieve this u ...

The lower text box on the page being covered by the virtual keyboard on IOS

Our website features a fixed header and footer with scrollable content. We have 20 text boxes on the page, but the ones at the bottom, like Zip and Telephone, are obscured by the iOS virtual keyboard that appears when a text box is clicked. If we could d ...

Experiencing difficulties installing fonts on Nuxt/Tailwind

I followed the instructions in this answer precisely, but my custom font class is still not working: tailwind.config.js: module.exports = { theme: { fontFamily: { "intro-regular": "intro-regular" }, extend: { ...

Is there a way to use node.js to retrieve a video in mp4 format?

My goal is to allow users to download a video from my AWS S3 bucket in MP4 format: app.get("/download_video", function(req,res) { filename = "s3.xxx.amazon.com/bucketname/folder/video_example.mp4"; // I'm unsure about the next steps }); Whil ...

Prevent form submission without JavaScript

While the issue is easy to grasp, it poses a challenge in implementation. I am encountering clients who disable their browser's Javascript by default, causing complications on my website. This is because my website sends ajax requests upon form submis ...

Sending an array of objects in JavaScript to a controller

I have a dynamic form that I'm trying to submit to my Controller. Instead of sending just a string or an array with data, I need to pass an array of objects using ajax request after building the Javascript Array. The challenge is that when I send only ...

using props as arguments for graphql mutation in react applications

Here is the structure of my code: interface MutationProps{ username: any, Mutation: any } const UseCustomMutation: React.FC<MutationProps> = (props: MutationProps) => { const [myFunction, {data, error}] = useMutation(props.Mutati ...

Ways to prevent recurring variables in Twitter bootstrap dialogues

I need assistance with deleting multiple links using ajax: <a id="id-1">link1</a> <a id="id-2">link2</a> <a id="id-3">link2</a> <a id="id-4">link2</a> ... This is the simplified version of my code: $(docum ...