In your code you are not checking whether the element is visible or not, you are checking whether it exists on the page.
From what I understand from your logic, the element always exists on the page, whether visible or not:
if ($('.buyButtonWrapper').length) { ... }
Never gets into the else
because the element is present in the DOM, only it is not visible to the user.
Solution
To check if the element is visible, in jQuery you can make use of the method .is()
(English) which will check the element "against" a selector, in this case the :visible
(English):
// Se "Comprar" está visivel
if ($('.buyButtonWrapper').is(':visible')) {
$('.bloco-flags-desconto .flagsWrapper').hide(); // div flags descontos
$('.bloco-flags-desconto .flagsWrapper2').show(); // div flag Indisponivel
$('.ui-accordion').hide(); // accordion de parcelamento
}
// Se "Comprar" está escondido
else {
$('.bloco-flags-desconto .flagsWrapper').show();
$('.bloco-flags-desconto .flagsWrapper2').hide();
$('.ui-accordion').show();
}
Your code can still be optimized for:
// colocar elementos em cache
var $descontos = $('.bloco-flags-desconto .flagsWrapper'), // div flags descontos
$indisponivel = $('.bloco-flags-desconto .flagsWrapper2'), // div flag Indisponivel
$parcelamento = $('.ui-accordion'); // accordion de parcelamento
if ($('.buyButtonWrapper').is(':visible')) {
// Se "Comprar" está visivel
$descontos.hide();
$indisponivel.show();
$parcelamento.hide();
}
else {
// Se "Comprar" está escondido
$descontos.show();
$indisponivel.hide();
$parcelamento.show();
}
The second version of the code is preferable because if you have to update the selectors, you only need to mess with the values of the variables, you don’t need to do multiple updates further down in the middle of the code.
Does it give an error in your code? Expected result, obtained result? I think it’s easier if you mount a Stacksnippet or Jsfiddle that demonstrates the problem in isolation (html and js basic).
– brasofilo
No error, but it does not show the expected result. I do not know if I can post the code because I am using the e-commerce platform.
– Steve Angello
I don’t see
unavailableProductDefault
in your code .js. Where is it?– Sergio
So Sergio, the code I’m using local the platform has a system that I can reverse Proxy and use the local files and with Livereload.
– Steve Angello