2

my check-boxes look like

<input type="checkbox" id="checkbox_1" name="artist" value="Yes">
<input type="checkbox" id="checkbox_2" name="song" value="Yes">

and my code is

$(document).ready( function() {
    $('#checkbox_1').click( function() {
        $('#checkbox_2').each( function() {
            this.checked = !this.checked;
        });
        $("#submit").click();
    });
});

problem is when i check checkbox_1 it doesnt stay checked (it stays deselected).. after $("#submit").click();

hope you get the ideea

ok ok ok ..

the desired effect is:

Step1: both checkboxes are cleared

Step2: if i select checkbox1 then both checkboxes are selected

Step2: if i deselect checkbox1 then both checkboxes are cleared again

get it ?

1
  • It looks like something else must be going on, or I misunderstood the question: Here's a fiddle that seems to work fine. Commented Aug 28, 2011 at 20:53

2 Answers 2

1
$(function () {
  $('#checkbox_1').click(function () {
    $('#checkbox_2').prop('checked', $(this).prop('checked'));
  });
});

You don't need the .each unless there is more then one element.

http://jsfiddle.net/t3zgx/2/

Sign up to request clarification or add additional context in comments.

2 Comments

how come there might be more than one element if you operate on Id not on Class?
hm.. i think something else interferes with my code , it seems that after $("#submit").click(); one checkbox stays checkd and the other cleared.. they belong to different forms ...
0
$(function () {
    var checkbox_one = $('#checkbox_1');
    var checkbox_two = $('#checkbox_2');

    checkbox_one.click(function () {
      if(checkbox_one.prop('checked'))
      {
          checkbox_two.prop('checked', 'checked');
      }
      else
      {
          checkbox_two.removeProp('checked');
      }
  });
});

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.