4

I have a page with checkboxes, one of which is a select all button. So when the user clicks select all, it checks all the other boxes, and when another box is clicked, it unchecks select all. Here is the code.

          $(document).ready(function(event){   
                 //alert('wtf');
            $('#mailSubscribeAll').click(function(event){
                 //alert('wtf');
                $('.mailingCheckbox').attr('checked','checked');
            });             

            $('.mailingCheckbox').bind('click',function(event){
                //alert('wtf');
                $('#mailSubscribeAll').removeAttr('checked');
            });

        });

So my page is loading jQuery fine, the first alert triggers. None of the click events give the alerts. I have double-checked my HTML is correct.

So am I using the wrong event for jQuery, or is there a possible conflict on my page with other javascript that isn't jQuery?

HTML snippet:

    <tr>
    <td class="CBT3" colspan="3" height="29">
        <input type="checkbox" class="mailingCheckbox" id="mailNewsAlerts"    value="1" {if $smarty.session.profile.mailNewsAlerts}checked{/if} onclick="subscribeMarkProfile()">
        <label for="mailNewsAlerts"><b>News Alerts</b> - <cite>The latest breaking news from XXXXXXX</cite></label>
    </td>
</tr>    
<tr>
    <td class="CBT3" colspan="3" height="29">
        <input type="checkbox" id="mailSubscribeAll"    value="1" {if $smarty.session.profile.mailSubscribeAll}checked{/if} >
        <label for="mailSubscribeAll"><b>Subscribe me to all XXXXX newsletters!</b> </label>
    </td>
</tr>    

I'm going to also add the part of the page with the checkboxes is not visible when the page loads, but it is there in the HTML. I show the area after a button is clicked, but when the page loads, at first these checkboxes is invisible.

tkotitan
  • 3,003
  • 2
  • 33
  • 37
  • Have the HTML snippet available? – Jerod Venema Oct 29 '09 at 17:46
  • added, this code is a mess, all in tables, and whoops I pasted the smarty template code, not the rendered HTML, but I checked and it renders fine – tkotitan Oct 29 '09 at 17:54
  • also, technically my input fields are not in a
    tag, if that matters.
    – tkotitan Oct 29 '09 at 17:57
  • click != change. Usually there are comments pointing out an error, like when incorrect advice is being given in an answer, like this one suggesting the click event: http://stackoverflow.com/a/3442342/664132 – basic6 Jan 09 '15 at 09:39

3 Answers3

1

It works as expected for me with the following code:

<div>
  <input type="checkbox" id="mailSubscribeAll"/>
  <input type="checkbox" class="mailingCheckbox"/>
  <input type="checkbox" class="mailingCheckbox"/>
</div>

<script type="text/javascript">
  $(document).ready(function(event){   
    $('#mailSubscribeAll').click(function(){
      $('.mailingCheckbox').attr('checked','checked');
    });             
    $('.mailingCheckbox').click(function(){
      $('#mailSubscribeAll').removeAttr('checked');
    });
  });    
</script>

Does this shed any light on your problem?

Jan Willem B
  • 3,787
  • 1
  • 25
  • 39
1
 $("input:checkbox #mailSubscribeAll").click(function(event){
   $('.mailingCheckbox').attr('checked','checked');
});  

It looks fine but could you try this code now.

Tarik
  • 79,711
  • 83
  • 236
  • 349
1

When you programmatically change the state of checkboxes you must also check the event change, click is not fired always:

$('.mailingCheckbox').bind('change', ...

since you have two functions you should do something like:

function changeMailingCheckbox(...) { ... }
$('.mailingCheckbox').bind('change', changeMailingCheckbox)
$('.mailingCheckbox').bind('click', changeMailingCheckbox)
Ciantic
  • 6,064
  • 4
  • 54
  • 49
  • Thanks for that, tried it, no luck. I think my page is such a mess of tables that maybe jQuery is losing it's mind looking at it? – tkotitan Oct 29 '09 at 18:20