Check all just selects the first foreach value

Asked

Viewed 334 times

2

Good people, I have the following code, and he was supposed to select all the values inside the foreach, but instead of doing it just selects the first value..

I tried to make a test of Submit that deletes the scenes only appear when you click on the checkboxes, but the same thing happened, only appeared if you selected the first one. But if you selected the first one and a few more, it would erase anyway, but only appear if the first one was selected..

<form action="" method="post">
<? foreach($itens as $myrow){ ?>
<a href="pagina.php?ID=<? echo $myrow['ID']; ?>"><? echo $myrow['Title']; ?></a>


<? echo $myrow['GDate']; ?>
?>
<? echo $myrow['Hour']; ?>
<div class="dashed-line"></div>
<input name="selector[]" id="testecb" type="checkbox" value="<?php echo $myrow['ID']; ?>" />
</div>
</div>
<?}?>
<input type="button" id="selectall-game-button" label="check all" value="Selecionar tudo"
><input type="submit" id="delete-game-button" value="Eliminar" />

<?
****************** AQUI ESTA A ACÇÃO QUE DELETA.. **************
}
} ?>

JS:

$('#selectall-game-button').click(function(){
        var chk = $(this).click('checked')?true:false;
        $('#testecb').attr('checked',chk);
});

Does anyone hate why this happens? : ss

  • $(this).click('checked')?true:false; should be .attr() instead of .click()...

  • You could actually simplify to $('#testecb').attr('checked', this.checked); only.

  • @Sergio could, but that wouldn’t change the checkbox status at all :)

  • Yes, because I want you to select everything when I click the button ..

  • Start by giving a different ID for each checkbox, you cannot repeat Ids in HTML. If you want, you can also put the same class on all to select more easily in jQuery (but this doesn’t eliminate the need for unique Ids).

1 answer

2


You have two problems: Multiple Ids and a JS error.

#1 - You cannot use multiple Ids in this case. CSS classes is the solution. This is because the selector $('#testecb') will only return the first element you find. I suggest you use $('. dashed-line input')

#2 - In the JS you have $(this).click('checked') but this element is an input and has no checked. So I guess this input type="button" should check all checkboxes...

So a suggestion would be:

$('#selectall-game-button').click(function(){
    $('[name="selector[]"]').prop('checked', true);
});

Note that this solution does not fix the multiple Ids you are generating in PHP. To fix this you could use in PHP:

<div class="dashed-line"></div>
<input name="selector[]" class="testecb" type="checkbox" value="<?php echo $myrow['ID']; ?>" />
</div>

and then the selector could already be $('.testecb').prop('checked', true);

  • Now you select nothing..

  • What I want is to select ALL the checkboxes..

  • @thecreator $('name="selector[]"').prop('checked', this.checked); does just that. If it’s not working create a jsFiddle with the rendered HTML to view your entire HTML.

  • http://jsfiddle.net/262z0jdp/1/

  • @thecreator I thought you wanted to select/de-select everything dependent on another checkbox being checked or not. If it’s just a button that selects all then it’s simpler... http://jsfiddle.net/262z0jdp/2/

  • What if this button has to de-select as well? Is it possible to do? And the delete button only appears when Some checkbox is selected.. If you could help me with that, I’d appreciate it!

  • @thecreator therefore, that was more or less the initial idea. Something like this: http://jsfiddle.net/262z0jdp/3/

  • But then the delete button is always visible, as I do to be visible only when 1 or more checkbox are selected?

  • @thecreator in this example the idea is to use the checkbox to select and to de-select, re-clicking on it.

  • Is it not possible to do this without checkbox? the select and de-select? Ex: type="button"

  • @thecreator yes, it is. Want 3 buttons or only two? the "Elemminar" is the one that de-selects?

  • Let me explain. I would like the "Select" Button to be a button (type="button") and to select and de-select. And the ELIMINATE button only appeared when 1 or more checkboxes were selected..

  • @thecreator ok, I see. So you want something like this: http://jsfiddle.net/xges18jq/ "Elemninar" should be hidden in the CSS at the beginning... you know how to do this?

  • Siim, display:none; Thank you!

  • @thecreator if you want you can mark the answer as accepted.

  • 1

    Sorry, I completely forgot ! Thanks again for all the patience and for helping me solve this !

Show 11 more comments

Browser other questions tagged

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