-3

I want to make HTML checkboxes have a certain set of CSS styling only when it's been selected. E.g. change colour or change text weight, etc. I am confused about whether I should use a function or if there is an existing method that I could use.

I have gone through these questions:

wazz
  • 4,953
  • 5
  • 20
  • 34

6 Answers6

3

You don't need any function for this, you can do this with good understanding of css selectors

You can try:

input[type="checkbox"]:checked+span {
    text-decoration: line-through;
    color:red;
}

and define your text box like

<input type="checkbox" ><span>Some text label for checkbox</span>

Here is an example

bali
  • 327
  • 1
  • 5
  • 14
  • 1
    yes but this is not the proper way to ask a question you better first give it a try as @DanStarns said – bali Oct 18 '19 at 19:16
  • 1
    and then search for what problem you faced, there are lots of answer to this question, as one mentioned by @TylerH above – bali Oct 18 '19 at 19:16
  • yeah I understand but as I said I am new to stack overflow, I wouldn't repeat it again – karthiksehghal Oct 18 '19 at 19:22
1

You can control the styling of checkboxes or other inputs based on their current state through CSS selectors like this:

input[type='checkbox'] {background-color:red;}

input[type='checkbox']:checked {background-color:blue;}
disc_code22
  • 109
  • 3
0

In JavaScript, use a function that you would call the onclick method on.

Example:

<div>
<p id ="color" onclick="changeFunction()">This text can change colors</p>
<button id="changeColor" onclick="changeFunction()"> Click Me</button>
</div>
<script>
function changeFunction(){
var words = document.getElementById('color');
words.style.color = 'red';
}
</script>

Here's a working Jsfiddle to show you this example: https://jsfiddle.net/z4gr8wn5/

This should give you a base idea of how you should go forward with figuring out what you need to find out.

King11
  • 1,239
  • 3
  • 9
  • 17
0

If you are looking on how to change a checkbox style when the user clicks then this will help you out https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_custom_checkbox

Benjamin Gil
  • 127
  • 1
  • 12
0

Making use of the :checked css pseudo-class you will be able to change the element style without using Javascript.

You can read more about it here.

/* Change color */
input[type="checkbox"]:checked {
  box-shadow: 0 0 0 3px red;
}

/* Change label color */
input:checked+label {
  color: blue;
}
<input type="checkbox" name="my-checkbox" id="example">
<label for="example">Check me!</label>
Williams A.
  • 683
  • 1
  • 8
  • 22
0

It might be that you want to toggle a class, and here is a example if that is the case.

<style>
  .mystyle {
    font-size: 25px;
  }
</style>
<div onClick='this.classList.toggle("mystyle");'>
  example1
</div>

or maybe add a class to an object you clicked.

<style>
  .mystyle {
    font-size: 25px;
  }
</style>
<div onClick='this.classList.add("mystyle");'>
  example2
</div>

or, if you really want to be in control you can send the clicked object to a function and manipulate it there, in any way you like.

<div onClick='myFn(this);'>
  example3
</div>
<script>
    function myFn(that) {
        that.style.color = "blue";
        that.innerHTML = 'You clicked me!';
    }
</script>
Griffin
  • 785
  • 5
  • 13