I’m still relatively new to javascript and jQuery and was just wondering this.
Suppose I have this HTML snippet:
<p id="disclaimer">
Disclaimer! </p>
<input type="button" id="hideButton" value="hide" />
I could hide the div in the following ways:
$(document).ready(function() {
$('#hideButton').click(function() {
if ($('#disclaimer').css('display') == 'none') {
$('#disclaimer').show();
$('#hideButton').val('hide');
} else {
$('#disclaimer').hide();
$('#hideButton').val('unhide');
}
})
});
OR
$(document).ready(function() {
$('#hideButton').click(function() {
if ($('#disclaimer').is(':visible')) {
$('#disclaimer').hide();
$('#hideButton').val('unhide');
} else {
$('#disclaimer').show();
$('#hideButton').val('hide');
}
})
});
My question is: Is there a preferred method of hiding the div or is it just a matter of personal preference?
i’d write that like this
or even
in response to the comment, here some points why i think this code is better