I'm trying to find a way to change a variable if a specific element is visible using JavaScript/jQuery. Here is my code:
function showSlides(n) {
var i;
var slides = null;
if (!$('#elementA').is(':visible')) {
slides = document.getElementsByClassName("setA")
}
else if (!$('#elementB').is(':visible')) {
slides = document.getElementsByClassName("setB")
}
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
}
Resuming, if element A is visible, I want to change the variable 'slides' to find all elements with the class '.setA'. Then it makes a slideshow out of all divs found with that class. If element A is not visible, I want my code to check if element B is visible and if so, change the variable 'slides' to find all elements with the class '.setB' etc.
The code works if I specify 'slides' without using 'if'.
I am quite new to this, can you help me out?
According to your question the code must be as follows.
if ($('#elementA').is(':visible')) {
slides = document.getElementsByClassName("setA")
}
if ($('#elementB').is(':visible')) {
slides = document.getElementsByClassName("setB")
}
There is no use of !
.