tags:

views:

38

answers:

2

How can i if #UseUsername type checkbox has been checked then toggle #div ?

+1  A: 

It's as easy as:

$('#UseUsername').change(function(){
  if($(this).is(':checked')){
    $('#div').show();
  } else {
    $('#div').hide();
  }
});

Additionally, you could fire this event when the page loads, so the div will disappear if the checkbox isn't checked.

// Show the div only if the checkbox is checked
function toggleDiv(){
  if($(this).is(':checked')){
    $('#div').show();
  } else {
    $('#div').hide();
  }
}

$(document).onload(function(){

  // Set change event to hide/show the div
  $('#UseUsername')
    .change(toggleDiv)
    .trigger('change');
});
Harmen
i use display: none so the div dont appears
Karem
+1  A: 

A very simple way would be like this:

$('#UseUsername').change(function(){
    $('#div').toggle(this.checked);  // show if it is checked, otherwise hide
});

Try it: http://jsfiddle.net/mHNuN/

patrick dw
This solution is much better than mine :]
Harmen