0

I have a checkbox and want to change the class of the button:

   let able = 'btn btn-secondary" disabled';
   
    function handleChange(e) {
     let isChecked = e.target.checked;
     if (isChecked===true){
         able = '"btn btn-primary"';
        console.log(able);
  }

console logs "btn btn-primary" which is correct , however the button is not changing at all and in the return:

  <input class="form-check-input" type="checkbox" id="gridCheck" onChange={e => handleChange(e)} />
  <button type='submit' class={able}>Register</button>

Can someone please let me know what I am doing wrong?

Thank you in advance

1 Answer 1

1

In React.js you should use state. I suggest you learn about the basics of React. For your case, this should do the trick:

JSX

import React, { useState } from 'react'; 

const [isChecked, setChecked] = useState(false);
   
const handleChange = (e) => {
     setChecked(e.target.checked);
}

HTML

<input class="form-check-input" type="checkbox" id="gridCheck" onChange={e => handleChange(e)} />
<button type='submit' disabled={!isChecked} class={`btn btn-${isChecked ? 'primary' : 'secondary'}`}>Register</button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.