In short, you are using two if()
and no return. That way, it will come in both. An "alternative" (gambiarra in my opinion) would be to put a return on if()
. It would look 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 if()
and else()
, like this:
$('#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');
}
});
This way it will enter the first or second, but never both.
A second option would be to use else if()
. This way you can have more than one condition, but only "will" enter the first accepted condition.
It would look like this:
$('#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()
at the end.
Remembering that you can also use a switch()
, depending on the case.