In short, you’re using two if()
and without any return. That way, it will enter both. An "alternative" (gambiarra in my opinion) would be to put a return on if()
. It would be something like this:
$('#btnFiltros').click(function () {
if ($('#divFiltros').css('visibility') === 'visible') {
$('#divFiltros').css('visibility', 'collapse');
$('#divFiltros').css('display', 'none');
return;
}
if ($('#divFiltros').css('visibility') === 'collapse') {
$('#divFiltros').css('visibility', 'visible');
$('#divFiltros').css('display', 'block');
}
});
However, there are two more better options.
The first is to use only the if()
and else()
, in this way:
$('#btnFiltros').click(function () {
if ($('#divFiltros').css('visibility') === 'visible') {
$('#divFiltros').css('visibility', 'collapse');
$('#divFiltros').css('display', 'none');
}
else{
$('#divFiltros').css('visibility', 'visible');
$('#divFiltros').css('display', 'block');
}
});
That way he’ll enter the first or the second, but never both.
A second option would be to use the else if()
. This way you can have more than one condition, but only "enter" in the first accepted condition.
Would look this way:
$('#btnFiltros').click(function () {
if ($('#divFiltros').css('visibility') === 'visible') {
$('#divFiltros').css('visibility', 'collapse');
$('#divFiltros').css('display', 'none');
}
else if ($('#divFiltros').css('visibility') === 'collapse') {
$('#divFiltros').css('visibility', 'visible');
$('#divFiltros').css('display', 'block');
}
});
You can still add more else if
or a else()
in the end.
Remember, you can also use a switch()
, depending on the case.