3

I have this piece of code:

<div class="quote-box-col14">
<label for="seo" class="quote-service-seo">SEO</label>
<input type="checkbox" name="seo" id="seo" value="Y" class="checkbox-seo" />
</div>

Someone can help with a JavaScript that adds class "selected" to when checkbox is selected?

Thanks

7 Answers 7

10

This jQuery will do it:

$('#seo').change(function(){
    if($(this).is(":checked")) {
        $(this).addClass("checked");
    } else {
        $(this).removeClass("checked");
    }
});

See this jQuery fiddle for a working example.

Edit:

If you need it to work in IE too, use the click event instead:

$('#seo').click(function(){
    if($(this).is(":checked")) {
        $(this).addClass("checked");
    } else {
        $(this).removeClass("checked");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

IIRC, in IE, the checkbox needs to be blurred before the change event gets fired. You are better off using click event.
5

And in pure JavaScript (for HTML5)

  document.getElementById ('seo').addEventListener ('click', function (ev) {
    ev.target.parentNode.classList[ ev.target.checked ? 'add' : 'remove'] ('selected');
  }, false);

There is also a classList.toggle function but there is always a danger that the checkbox and the classList might lose synchronism.

Comments

2

You can infer from this: jQuery toggleClass and check checkbox

But essentially, you can use toggleClass to toggle a specific CSS class:

$("#seo").click(function() {
    $(this).toggleClass("cssClassToToggle");
});

Or use addClass to add a new class only:

$("#seo").click(function() {
    $(this).addClass("cssClassToToggle");
});

If you are talking a range of checkboxes, you can target with $(":checkbox") too.

Comments

2

Here's pure js that will work for adding and removing the selected class when any checkbox is checked or unchecked.

Working example: http://jsfiddle.net/S9tSS/

var inputs = document.getElementsByTagName("input");
var checkboxes = [];

for (var i = 0; i < inputs.length; i++) {
  if (inputs[i].type == "checkbox") {
    checkboxes.push(inputs[i]);
    if (inputs[i].checked) {
      checked.push(inputs[i]);
    }
  }
}

for (var j = 0; j < checkboxes.length; j++) {
    checkboxes[j].onclick = function() {
        if (this.checked == true) {
            this.className += " selected";
        } else {
            removeClassName(this, 'selected');
        }
    }
}

function removeClassName(e,t) {
    if (typeof e == "string") {
        e = xGetElementById(e);
    }
    var ec = ' ' + e.className.replace(/^\s+|\s+$/g,'') + ' ';
    var nc = ec;
    if (ec.indexOf(' '+t+' ') != -1) {
        nc = ec.replace(' ' + t + ' ',' ');
    }
    e.className = nc.replace(/^\s+|\s+$/g,'');
    return true;
}

Sources: ...well... I would post them, but I'm a new use an can't post more than two links.

Comments

1

With jQuery:

$('input[type="checkbox"]').change(function() {
    if (this.checked) {
        $(this).addClass('checked');
    } else {
        $(this).removeClass('checked');
    }
});

2 Comments

I have more than one checkbox in the page. How can I select them by ID?
@Andrei See my answer above. You can refer to IDs and it is all jQuery.
1
$('#seo').click(function() {
    if (this.checked) {
        $(this).addClass('selected');
    } else {
        $(this).removeClass('selected');
    }
});

Comments

1
$('#seo').click(function(){
    $(this).toggleClass('selected', this.checked);
});

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.