How to enable/disable a Div when clicking the checkbox

Asked

Viewed 548 times

-8

How to turn on and off a div by clicking on checkbox

$("#chkBloqueio").click(function() {
  if ($("#chkBloqueio").attr('checked') == true) {
    $("#divBloqueio").attr('display', 'inline');
  } else {
    $("#divBloqueio").attr('display', 'none');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divBloqueio" runat="server" style="display:none"></div>

  • Hello @Matheus. You need to give more detail in your question: what you want, what difficulties, etc, otherwise we will not be able to help you.

  • Set "Disable". Would you hide it? Remove it from the DOM? Make all <input> within it disabled?

  • The "on/off" would make it visible in the DOM, when the checkbox is checked the Div will be display:inline. (Sorry I didn’t clear it up initially)

1 answer

1


Would look like this:

$('#chkBloqueio').on('change', function () {
    if ($(this).is(':checked')) {
        $('#divBloqueio').css('display', 'block');
    }
    else {
        $("#divBloqueio").css('display', 'none');
    }
});
  • 2

    It would be more performative to use if (this.checked) because it would not need to convert to a jQuery object and is native.

  • Thanks, it worked out.

Browser other questions tagged

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