How to switch between true and false checked input?

Asked

Viewed 12,405 times

4

I have this code and would like to switch between true and false the attribute checked input. Strangely is catching the id of label because the web designer superimposed the label at the input.

(function($){
    $("label").click(
        function(){
            var idcheck = $(this).attr("for");
            $("input[id="+idcheck+"]").attr("checked", "checked");
        }
    );
})(jQuery);
  • As far as I can remember, in jQuery version 1.6 or higher checked is treated as property and not as attribute - that is, you get and change with the method prop and not attr.

8 answers

16


I would use the following code:

/* Não precisa de JavaScript! */

That is, no code! If your label already has the attribute for pointing to input, it is not necessary to use JS. The default behavior of clicking on the label is to just reverse the checkbox state. Example Jsfiddle

The code you posted seems to have been created to try to prevent checkbox from being unchecked (every click ends with it checked).

  • 1

    @Kaduamaral Thank you for jsfiddle!

4

use $("input[id="+idcheck+"]").prop('checked', false);

  • I rephrased the question because you misunderstood due to what was written. Thank you for reconsidering a new answer and great Sunday.

4

Try this:

Teste: <input type="checkbox" id="ck" checked/>

$(document).ready(function(){

   $("#ck").attr('checked', false); 

});

4

I suggest removing all attribute checked.

(function ($) {
    $("label").click(function () {
        var idcheck = $(this).attr("for");
        $("input[id=" + idcheck + "]").removeAttr('checked'); // <--
    });
})(jQuery);

Or similar alternative (give value false):

(function ($) {
    $("label").click(function () {
        var idcheck = $(this).attr("for");
        $("input[id=" + idcheck + "]").prop("checked", "false"); // <--
    });
})(jQuery);
  • I rephrased the question because you misunderstood due to what was written. Thank you for reconsidering a new answer and great Sunday.

3

If the webdesigner didn’t do "extra work" you weren’t supposed to have a problem and you didn’t need a script to set the check. But if it really is easier to adapt the script than having to revise the code...

Do not use attr. Because attributes do not work well with "living properties" instead use prop() as suggested by our friend Sergio and wriel.

$("label").click(function () {
     var idcheck = $(this).attr("for");
     $("input[id=" + idcheck + "]").prop("checked", "false"); // <--
});

If you use attr, after changing the status sometimes it will stop working. I’ve had this problem before, and I’m putting you ahead of time to avoid a headache. The prop, will always bring the current status, already the attr can get lost in memory, and not reflect the current moment.

Here are more details

2

You can use this same code only with a few changes at the time you change the "checked".

(function($){
    $("label").click(
        function(){
            var idcheck = $(this).attr("for");

            var checkedStatus = $("input[id="+idcheck+"]").attr("checked"); // Aqui eu pego o valor atual do checked no input
            $("input[id="+idcheck+"]").attr("checked", !checkedStatus); // e atribuo um novo valor invertendo o valor antigo
        }
    );
})(jQuery);

In that case it will always invert the checked input value:

  • If the input checked is true it will set the new value to false.
  • If the input checked is false it will set the new value to true.

1

Try it this way:

$( "input[type='checkbox']:odd" ).attr( "checked", "true" );

0

After catching a little I managed to solve in a cool way: using denial.

document.getElementById("Checkbox").checked = !document.getElementById("contatoCheckbox").checked;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.