I've been working on a script to toggle between two divs onClick - opening one while closing the other if it's already open. I'm fairly new to jQuery and Javascript, having mainly worked with HTML/CSS.
Script I found:
$(document).ready(function () {
var expanded = false;
var collapsed = true;
$(".expanderHead").click(function () {
if (expanded == true) {
expanded = false;
collapsed = true;
} else {
expanded = true;
collapsed = false;
}
if (expanded == true) {
$(".expanderSign").html("-");
$(".expanderContent").slideToggle();
}
if (collapsed == true) {
$(".expanderSign").html("+");
$(".expanderContent").slideToggle();
}
});
});
My attempt:
$(document).ready(function () {
var expanded = false;
var collapsed = true;
$(".infoexpanderHead1").click(function () {
if (expanded == true) {
expanded = false;
collapsed = true;
} else {
expanded = true;
collapsed = false;
}
if (expanded == true) {
$(".infoexpanderSign1").html("-");
$(".infoexpanderContent1").slideToggle();
$(".infoexpanderContent2").slideToggle();
}
if (collapsed == true) {
$(".infoexpanderSign1").html("+");
$(".infoexpanderContent1").slideToggle();
$(".infoexpanderContent1").slideToggle();
}
});
$(document).ready(function () {
var expanded = true;
var collapsed = true;
$(".infoexpanderHead2").click(function () {
if (expanded == true) {
expanded = false;
collapsed = true;
} else {
expanded = true;
collapsed = false;
}
if (expanded == true) {
$(".infoexpanderSign2").html("-");
$(".infoexpanderContent2").slideToggle();
}
if (collapsed == true) {
$(".infoexpanderSign2").html("+");
$(".infoexpanderContent2").slideToggle();
}
});
});
The goal is to have different content in each infoexpandercontent
. When 'Content 1' is expanded, 'Content 2' should collapse. Is there a way to add a CSS class to the 'expanded' span for a smoother transition?
If you need more clarity or have any suggestions, please let me know!
Thank you for any help!