1

I have a html elements (Using MVC3 Razor)

       <h2 class="price">
              @Html.LabelFor(vm => vm.Price)
              @Html.Encode(@Model.Price)
       </h2>
        <div id="checkbox-price">
              @Html.CheckBoxFor(vm => vm.IncludeToPayment) Include in payment.
        </div>

How can I change the style of the h2 element, (in this case I want to text-decoration: line-through the items inside) when the user uncheck/check the Checkbox using JQUery?

Thanks in advance.

2 Answers 2

5
$('#checkbox-price input:checkbox').change(function(){
  if (!$(this).is(':checked')) {
     $('.price').css('text-decoration', 'line-through')
  } else {
     $('.price').css('text-decoration', 'none')
  }
})

or

  $('#checkbox-price input:checkbox').change(function() { 
            // or $(`input[type="checkbox"]')
            var $obj = $('.price');
            if (!$(this).is(':checked')) {
              $obj.css('text-decoration', 'line-through'); //or addClass                
            } else {
              $obj.css('text-decoration', 'none'); // or addClass                
            }
        })
Sign up to request clarification or add additional context in comments.

3 Comments

how about if i have many checkboxes? is this also triggers if I checked the other checkboxes? sorry Im a beginner in JQuery
@csharplinc in that case the code should be modified, it depends on the structure of the markup.
@undefined ++1 for the quick-ness :P, Can do a minor improvement.
2

Here is a simple code and live demo.

This applies for all the check boxes

$('input[type="checkbox"]').click(function() {
    if (this.checked) {
        //alert('Checked');
        $("h2").addClass('newclass');
    } else {
        //alert('Unchecked');
        $("h2").removeClass('newclass');
    }
});​

If you want to apply for specific check box use below

$('input[type="checkbox"]') to $('input.someclass)

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.