Does something like jQuery.toggle(boolean) exist? Does something like jQuery.toggle(boolean) exist? jquery jquery

Does something like jQuery.toggle(boolean) exist?


Ok, so I am an idiot and need to RTM before I ask questions.

jQuery.toggle() allows you to do this out of the box.

$("button").click(function() {  var condition = $("#agree").is(":checked") && $("#name").val() != "" );  $("#mydiv").toggle(condition);});


First, lets see if I understand what you want to do correctly...You want to look at the state of a checkbox(checked or not) and hide or show a second div based on the status of that value.

Define this style:

.noDisplay {    display:none;}

Use this JavaScript:

$("button").click(function() {  $("#mydiv").toggleClass("noDisplay", $("#name").val() == "");});

The documentation from jQuery on it can be found here:http://api.jquery.com/toggleClass/


You could write the function yourself.

function toggleIf(element, condition) {    if (condition) { element.show(); }    else { element.hide(); }}

Then use it like this:

toggleIf($("button"), $("#agree").is(":checked") && $("#name").val() != "");