Make the checkbox font-weight bold when it is checked

Attempting to apply custom CSS to enhance the appearance of checkboxes using input and label elements. The goal is to make the font-weight bold when a user clicks on the checkbox, and then revert it back to regular if clicked again. Currently stuck on this styling challenge!

Check out the Fiddle

$(function() {
  var action = 1;
  $('.control--checkbox input').on("click", handleFontWeight);

  function handleFontWeight() {
    if (action == 1) {
      adjustFontWeight("bold");
      action = 2;
    } else {
      adjustFontWeight("400");
      action = 1;
    }
  }

  function adjustFontWeight(val) {
    $('.control--checkbox').css({
      fontWeight: val
    })
  }
});
.control-group {
  display: inline-block;
  vertical-align: top;
}

/* Rest of the CSS code remains unchanged */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="facet-group">
  <h3>Operating System</h3>
   /* HTML structure for checkboxes remains intact */
</div>

Answer №1

Give this a shot

$(function() {

   $('.control--checkbox input').on("change", function() {
    if ($(this).data('isbold')) {
      $(this).parent().css('font-weight', 'normal') 
      $(this).data('isbold', false)
    } else {
      $(this).parent().css('font-weight', 'bold')
      $(this).data('isbold', true)
    }
  });

});

Answer №2

The need for the action checks is unnecessary. It's possible to verify if they are checked by utilizing event.target.checked, which will provide either true or false values.

$(function () {

  

  $('.control--checkbox input').on("change", goBold);

  function goBold(event) {
      if(event.target.checked) {
        fontWeight($(this), "bold");
      } else {
        fontWeight($(this), "400");
      }
  }

  function fontWeight(element, val) {
    element.parent().css({fontWeight: val})
  }
});
.control-group {
display: inline-block;
vertical-align: top;
}

.control-group .control {
display: block;
position: relative;
cursor: pointer;
font-size: 18px;
padding-left: 30px;
  margin: 15px 0;
}

....(CSS styling continued)....

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="facet-group">
  <h3>Operating System</h3>
  <div class="control-group">
    <label class="control control--checkbox">Apple iOS (42)
      <input type="checkbox" />
      <div class="control__indicator"></div>
    </label>

    ... (HTML code for checkboxes) ...
    
  </div>
</div>

Note: It's also achievable with pure CSS alone.

Answer №3

Utilize this.checked to verify its value...if checked, apply font-weight:bold using css() in jQuery

$(".control--checkbox input").on("change", function() {
  var font = this.checked === true ? "bold" : "normal";
  $(this).closest(".control--checkbox").css("font-weight", font);
})
.control-group {
  display: inline-block;
  vertical-align: top;
}

/* CSS Styles */
.control-group .control {
  /* styling properties */
}

// More CSS styles here...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="facet-group">
  <h3>Operating System</h3>
  <div class="control-group">
    <label class="control control--checkbox">Apple iOS (42)
      <input type="checkbox" />
      <div class="control__indicator"></div>
    </label>
    <label class="control control--checkbox">Android (20)
      <input type="checkbox" />
      <div class="control__indicator"></div>
    </label>
    <label class="control control--checkbox">Windows (8)
      <input type="checkbox" />
      <div class="control__indicator"></div>
    </label>
  </div>
</div>


You can achieve this by using only css if you are allowed to make some adjustments to your markup like wrapping the text inside the .control__indicator div and modifying the css as well...

.control-group {
  display: inline-block;
  vertical-align: top;
}

/* Additional CSS Styles */

<div class="facet-group">
  <h3>Operating System</h3>
  <div class="control-group">
    <label class="control control--checkbox">
      <input type="checkbox" />
      <div class="control__indicator">Apple iOS (42)</div>
    </label>
    <label class="control control--checkbox">
      <input type="checkbox" />
      <div class="control__indicator">Android (20)</div>
    </label>
    <label class="control control--checkbox">
      <input type="checkbox" />
      <div class="control__indicator">Windows (8)</div>
    </label>
  </div>
</div>

Answer №4

You can achieve the same outcome using only CSS, without needing any JavaScript or jQuery.

Live Example

fieldset {
  box-shadow: 4px 4px 3px 2px rgba(0, 0, 0, 0.3);
  padding: 0 0 0 25px
}

legend {
  font-family: Verdana;
  font-size: 36px;
  text-shadow: 2px 2px 3px rgba(0, 0, 0, 0.6);
}

.chx {
  display: none
}

label {
  display: block;
  font-family: 'Arial';
  font-size: 32px;
  font-weight: 400;
  margin: 10px 0 0 0;
  text-shadow: 1px 2px 3px rgba(0, 0, 0, 0.6);
}

.chx:checked+label {
  font-weight: 900
}

.chx+label b {
  display: inline-block;
  width: 18px;
  height: 18px;
  box-shadow: 1px 1px 2px 1px rgba(0, 0, 0, 0.4);
}

.chx:checked+label b {
  background: forestgreen;
}

.chx:checked+label b::before {
  display: block;
  content: '✔';
  font-family: 'Arial Black';
  font-size: 32px;
  font-weight: 900;
  margin-top: -20px;
}
<fieldset>

  <legend>Operating Systems</legend>

  <input id='chx0' class='chx' type='checkbox'>
  <label for='chx0'><b></b> Apple iOS(42)</label><br>

  <input id='chx1' class='chx' type='checkbox'>
  <label for='chx1'><b></b> Android(20)</label><br>

  <input id='chx2' class='chx' type='checkbox'>
  <label for='chx2'><b></b> Windows(8)</label><br>

</fieldset>

Answer №5

Below is a code snippet that can be useful for you:

// This code will trigger on checkbox change event.
 $('.control--checkbox input').on('change',function(){
      // Get the parent div of the current checkbox
      var $parentDiv = $(this).parent();
      // Check if it is checked or not
      if(this.checked){
           // Execute this code if it is checked
           $parentDiv.css({fontWeight: "bold"})
      } else {
           // Execute this code if it is not checked
           $parentDiv.css({fontWeight: "400"})
      }
});
.control-group {
display: inline-block;
vertical-align: top;
}

/* CSS styles for the control group */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="facet-group">
  <h3>Operating System</h3>
  <div class="control-group">
    /* HTML structure for checkboxes */
  </div>
</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

Guide to highlighting manually selected months in the monthpicker by utilizing the DoCheck function in Angular

I'm facing an issue and I could really use some assistance. The problem seems quite straightforward, but I've hit a roadblock. I have even created a stackblitz to showcase the problem, but let me explain it first. So, I've developed my own t ...

The value stored in $_POST['valuename'] is not being retrieved

Having recently delved into ajax, I am encountering some difficulties in making it function properly. The objective of the code is to send two variables from JavaScript to PHP and then simply echo them back as a string. However, instead of receiving the e ...

The reason the section's height is set to 0 is due to the absolute positioning of the div

Currently, I have a section tag with three divs inside that are positioned absolute (used for the isotope plugin). <section id="section1" class="cd-section"> // pos. static/relative has height 0px <div class="itemIso"> // pos. absolute, he ...

Utilizing the smallslider feature through jQuery/JavaScript operations

I've recently embarked on the journey of learning JavaScript/jQuery. I've been attempting to incorporate this cool effect, but unfortunately, I'm facing some difficulties with it: My goal is to understand how to execute this effect using Ja ...

Angular is known for sending only the fields that have changed to the update method

I need help with optimizing my save method. When a user clicks SAVE, I only want to send the fields that have been changed instead of all 50+ fields on the page. This will reduce the amount of data being sent every time. Api.Admin.update({ obsoleteDat ...

Strange Safari 15 bug causing unexpected transformations

Hey there, I'm a div with animation on scroll using GSAP. On Safari 15 (no issues below 15), something strange is happening - parts of the letters are leaving an afterimage on the sides. The code snippet I am using for this animation is: this.$refs.l ...

UI-Bootstrap selectively adds modal backdrop to specific elements

I have encountered a unique situation with my application where I am displaying a UI-Bootstrap modal inside a div, positioned above a navigation bar. Typically, when the modal is launched, a backdrop (dimmer) covers the entire background. Interestingly, my ...

Issue with CSS in Internet Explorer 7

CSS CODE: .search { float: left; width: 100%; display: block; } .search ul.tabs { height: 23px; margin-top: 50px; padding: 0px; } /* FF ONLY */ .search ul.tabs, x:-moz-any-link { height: 26px; margin-top: 50px; padding: 0px; } .search ul.tabs ...

Creating the perfect layout with CSS: A step-by-step guide

As I work on refining my layout to achieve the desired look, let me share my initial idea before delving into the issue at hand. Currently, I am attempting to create something similar to this: https://i.stack.imgur.com/rtXwm.png This is how it appears a ...

Having trouble getting the CSS Transform property to be affected by ScrollTop

Hello everyone, I am attempting to create a ball that has a 50% radius and rolls left based on the user's scroll movements. So far, I have successfully made it move left in relation to ScrollTop, but unfortunately, I cannot get the transform property ...

Grasping the idea of elevating state in React

I can't figure out why the setPostList([...postList, post]) is not working as expected in my code. My attempts to lift the state up have failed. What could be causing this issue? The postList array doesn't seem to be updating properly. I'v ...

Anomaly with Responsive Images in Internet Explorer 7

Something unusual has come up. In the process of developing a WordPress theme, I needed it to be completely responsive. I decided to create a "gallery" consisting of rows with 3 thumbnails each, which would open in a lightbox when clicked. To achieve this ...

The concept of using the `map` method within a

Hi there, I could use some assistance with a tricky issue I'm facing. My current task involves rendering a cart object that includes product names, prices, and quantities. Each product can have its own set of product options stored as an array of ob ...

Click the mouse to create a unique path from the list items within the <ul> <li> using jQuery

My current listing contains various files and folders: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fon ...

Show flexibility in the grandchildren of a row layout

My goal is to achieve a layout similar to the image below using the flex "system", but I am facing a challenge with some generated directives that are inserted between my layout div and the flex containers: https://i.sstatic.net/nq4Ve.png <div id="i-c ...

Utilizing JSON strings within an onclick function

Hey there, currently I am working on sending an encoded JSON through the onclick attribute. However, I am facing a challenge because the JSON contains strings with a lot of apostrophes and quotes which end up closing the quotes in the onclick attribute. Up ...

Issue with draggable div containing gmap not functioning on mobile browsers

Is it possible to make a specific div draggable without dragging the content inside, such as a gmap widget? I have tried implementing this functionality in my code and it works on a computer browser but not on a mobile browser. Here is the simplified versi ...

Utilizing the datepicker options function within a different function

I'm working on a function that utilizes a promise to retrieve data from an asynchronous ajax call: $("#mySelect").on('change', function() { var mySelectValue = $('#mySelect').val(); var promise = getAvailableDates(mySe ...

Using the @ symbol in jQuery within an MVC framework can be achieved by first ensuring that

I'm attempting to incorporate the @ symbol into a JavaScript if condition, but I keep receiving an error. if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) { alert('yes'); ...

Organize the array following the guidelines of a card game with a versatile approach

deck = ['Jack', 8, 2, 6, 'King', 5, 3, 'Queen', "Jack", "Queen", "King"] <!- Desired Result = [2,3,5,6,8,'Jack','Queen','King'] Explore the challenge: Arrange the ...