1

I have simple function in which I change the default styling of text when a checkbox is checked, and change the styling back to default when checkbox gets unchecked.

The terms.style = ""; should reset the style back to default, but for some reasons it doesn't, and I have absolutely no idea why. I know that the else scope is performed when checkbox gets unchecked, as I have tested it with manually entering different style.

const form = document.getElementById('form');
const checkBox = form.querySelector('input[name=termsCheckBox]');

checkBox.addEventListener('click', function(){
    const terms = document.getElementById('termsText');
    if (checkBox.checked){
        terms.style = "color: black; font-weight: normal";
    } else {
        terms.style = "";
    }
});//end of function
8
  • 3
    Your code work jsfiddle.net/d6rg1v25 Commented Dec 7, 2018 at 15:05
  • element.style is a read-only Object. Either use element.setAttribute('style', 'color: black; font-weight: normal') or use element.style.cssText = 'color: black; font-weight: normal'. Commented Dec 7, 2018 at 15:08
  • Sadly, neither of them helps. Commented Dec 7, 2018 at 15:15
  • Has your element some CSS in style attribute that you want to back to default? Commented Dec 7, 2018 at 15:18
  • Yes, the default style is color: red and font-weight: bold. I know I could insert these styles manually in js, but then it would be wrong from design perspective. Commented Dec 7, 2018 at 15:20

2 Answers 2

2

You can get inline CSS in style attribute of element using getAttribute() and store it in variable and on check/uncheck of checkbox insert and remove it from style attribute

var checkBox = document.querySelector('#form input[name=termsCheckBox]'),
    terms = document.getElementById('termsText'),
    style = terms.getAttribute('style');

checkBox.addEventListener('click', function(){
  if (checkBox.checked)
    terms.style.cssText = "color:black; font-weight:normal";
  else
    terms.style.cssText = style;
});
<form id="form">
  <input type="checkbox" name="termsCheckBox">
  <textarea id="termsText" style="color:red; font-weight:bold">termsText</textarea>
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

Why not just terms.style = style; in else statement ?
1

According to MDN:

Styles should not be set by assigning a string directly to the style property (as in elt.style = "color: blue;")

Correct way would be:

checkBox.addEventListener('click', function(){
    const terms = document.getElementById('termsText');

    if(checkBox.checked){
        terms.style.color = "black";
        terms.style.fontWeight = "normal";
    }else{
        terms.style.color = "";
        terms.style.fontWeight = "";
    }
});

1 Comment

Hi Dmitriy. I changed the code to yours, but sadly the style still doesn't reset.

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.