-3

I have an event listener that listens to a series of checkboxes, I am able to add a property when the box is checked, but when the box is unchecked I am not able to remove the property. what can be done to make it work properly?

so when i use this: $(this).val(':checked'); then I get this

How do I remove the value=":checked" ?

<script>
    $('.programs').click(function () {
        if ($(this).val(':checked') == "checked") {
            $(this).prop('checked', false);
        } else {
            $(this).val(':checked');
        }
    });
</script>
Ganesh Babu
  • 3,482
  • 10
  • 33
  • 63
jpavlov
  • 2,185
  • 8
  • 42
  • 57

2 Answers2

3

Use:

$('.programs').click(function () {
    if ($(this).is(':checked')) {
        $(this).prop("value", "Some thing");
        // or $(this).next("label").value("Some thing");
    } else {
        $(this).prop("value", "Some other thing")
        // or $(this).next("label").value("Some other thing");
    }
});

Using is jQuery method. I assume you are trying to check whether an element is checked or not, and not change its state instead.

Gaurang Tandon
  • 6,110
  • 10
  • 44
  • 79
  • this just logs it to the console, I am setting the value and need to remove the value when it is unchecked – jpavlov Apr 09 '14 at 12:57
  • @jpavlov I edited my answer. First line, changes the property `value`, while the other changes the text visible on screen. – Gaurang Tandon Apr 09 '14 at 13:02
1

Try this:

 $('.programs').click(function () {
        if ($(this).is(':checked')) {
            console.log("checked");
        } else {
            console.log("Uncheked");
        }
    });
Kiran
  • 19,739
  • 10
  • 64
  • 98