Toggle checkbox when clicking on a li (and toggling a class) with jQuery

Hello,
I have this HTML:
<ul id="press">
  <li class="selected">
    <label>
      <input name="ip_press[]" type="checkbox" value="1" checked="checked"  />
      <strong>First text</strong></label>
    <div>27/03/2011</div>
  </li>
  <li>
    <label>
      <input name="ip_press[]" type="checkbox" value="2"  />
      <strong>Other text</strong></label>
    <div>23/03/2011</div>
  </li>
  <li class="selected">
    <label>
      <input name="ip_press[]" type="checkbox" value="4" checked="checked"  />
      <strong>Another text</strong></label>
    <div>17/03/2011</div>
  </li>
</ul>

Open in new window

And this jQuery snippet:
$('#press li label').click(function(){
	var checkbox = $(this).children('input[type=checkbox]');
	if (checkbox.is(':checked')) {
		$(this).parent('li').addClass('selected');
	} else {
		$(this).parent('li').removeClass('selected');
	}
})

Open in new window


All is fine: when I click on the <label>, che corresponding checkbox is toggled and the class "selected" too
But what if I want to click on the whole <li> to toggle the checkbox and the class?

Here's a live example:
http://jsfiddle.net/TeWsw/2/

Thanks in advance for your help
ivanhalenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

khan_webguruCommented:
Hello Bro!

Use the following code

 
$('#press li').click(function(){
        
        var label = $(this).find(":first");
        var checkbox = $(label).children('input[type=checkbox]');
        if (checkbox.is(':checked')) {
            $(this).removeClass('selected');
            checkbox[0].checked = false;
        } else {
            $(this).addClass('selected');
            checkbox[0].checked = true;
        }
    })

Open in new window


Hope thats what you want.

Regards,

Asif Ahmed Khan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ivanhalenAuthor Commented:
Perfect, thanks!!! :-)
0
khan_webguruCommented:
My Pleasure :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Operations

From novice to tech pro — start learning today.