This question is similar but does not help.
The jQuery docs indicate that the $.toggle
function accepts a Boolean parameter:
The second version of the method accepts a Boolean parameter. If this parameter is true, then the matched elements are shown; if false, the elements are hidden.
However, passing a truthy (non-Boolean) value doesn't seem to get recognized. The code below alternates between showing/hiding the red box even though the toggle
function is passed a truthy value.
Why doesn't the truthy value work?
var imageURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABBoAAAK8CAYAAABBSdyWAAAgAElEQVR4Ady9+bMcyXHn6ZWZdbwbaDSAvthNiocoiuRobU2/6P//ZW3X1mZNGo6GktjsE2gAD8c76q7Ktc/X41uZr/DQYLObnNEEUC8y4/Tw8PDw8PCIHPzmt//QRs9tBhFtm0H4g8FgF+vwqqrCv/V6rfh+ul2GP+FhsM387XYQbTuISlVXUdd11";
$(document).ready(function() {
$(".box").on("click", function() {
$("#redBox").toggle(imageURL);
$("#curURL").text(imageURL);
});
});
.box {
position: fixed;
width: 200px;
height: 200px;
background: red;
cursor: pointer;
}
#redBox {
background: red;
}
#blueBox {
background: blue;
}
#curURL {
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="blueBox" class="box"></div>
<div id="redBox" class="box"></div>
<div id="curURL"></div>