Checkbox gets bugged MARK ALL -WITHDRAW SELECTION -Special case;

Asked

Viewed 76 times

2

Hello guys my goal is to mark and uncheck checkboxs until then all right look at print: OLHEM ALI NO SELECIONAR TODOS E RETIRAR SELEÇÃO

I press to select all and the script selects ALL I click to uncheck it unchecks ALL until then all right.

The PROBLEM is that if I go with the mouse arrow and click on a checkbox of those and remove the selection and then click on select all of them ARE NOT SELECTED BY THE TOOL SELECT ALL THAT I CREATED. Do you understand?

Kind of:

1- I clicked on select all, now ta everyone selected as in the image.

2-I choose the first checkbox and clear.

3-Now I will click on select all.

4-The select all does not mark my 1 checkbox. Understands agr?

my script is this:

//AQUI É PRA SELECIONAR TODOS OS CHECKBOX AMIGOS O BOTÃO AZUL LA ENCIMA SELECIONAR TODOS
$('.selecionadinho').bind('click',function() {

            $('.checkboxs').each( function() {

                $(this).attr('checked',true);

            }
            );  

    });


//AQUI ESSE CODIGO VAI RETIRAR TODOS OS CHECKBOX AZUL LA ENCIMA RETIRAR SELEÇÃO DE TODOS
        $('.retirar').bind('click',function() {

            $('.checkboxs').each( function() {

                $(this).attr('checked',false);

            }
            );  

    });

1 answer

1

Two things to fix in your code:

1. Change .bind for .on, because .bind() is no longer used, has been discontinued in jQuery 3.0.

$('.selecionadinho').on('click',function() {...
$('.retirar').on('click',function() {...

2. Do not use .attr to change boolean value true and false of input. Use .prop:

$(this).prop('checked',false); ou $(this).prop('checked',true);

With these changes your code will work round.

SUGGESTION to improve the code:

Instead of using two functions for this, you can use only one:

Place the same "SELECT ALL" and "SELECT ALL" links class="sel_tudo_nada", and replace its two functions with only this:

$('.sel_tudo_nada').on('click',function(){
   var checar = $(this).index() == 0 ? true : false;
   $('.checkboxs').each( function() {
      $(this).prop('checked',checar);
   });
});

UPDATING

The code:

var checar = $(this).index() == 0 ? true : false;

is an abbreviated form of:

// $(this).index() é o índice do link clicado,
// onde 0 é o primeiro e 1 seria o segundo
if( $(this).index() == 0 ){
   checar = true;
}else{
   checar = false;
}
  • Thanks! Now the suggestion of your improvement not accepted for anyone few people would understand that there , much less me, I did not understand any of this part $(this). index() == 0 ? true : false;

  • A difficult little code costs much more than a big friend, but you helped me mt to since yesterday without knowing what to do.

  • valeuuuuuuuuuuuuuuuuuuuuuu

  • @Smart programmer I put an update on the answer that explains the code thing you didn’t understand. Abs!

  • @Smart programmer If the answer was helpful and helped, please check ✔

Browser other questions tagged

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