I have a switch that changes parent class to a div, and based on that parent class, the links inside that parent do different things. I wrote it like it's CSS :D
// if the parent is .major, do this:
$(".major .menu a").click(function(e){
$(".minor-result").removeClass("act");
$(".major-result").addClass("act");
e.preventDefault();
});
// if the parent is .minor, do this:
$(".minor .menu a").click(function(e){
$(".major-result").removeClass("act");
$(".minor-result").addClass("act");
e.preventDefault();
});
Also, there will be seven different parent classes. It can't be a toggle and I really need those add/remove rules for child elements.
I understand that jQuery is binding that function to the existing parent-child combo and changing parent classes doesn't change the function.
Edit: One cleaver solution would be to put 7 unique links inside each menu li and those links would be display: block/none with CSS, based on parent class. If each link has only one function, there's no longer a problem. But maybe jQuery can simplify it by swapping 7 options.
// swap classes for #scale
$(".make-minor a").click(function(e){
$("#scale").removeClass("major");
$("#scale").addClass("minor");
e.preventDefault();
});
$(".make-major a").click(function(e){
$("#scale").removeClass("minor");
$("#scale").addClass("major");
e.preventDefault();
});
// here is the part I struggle with:
// if the parent is .major, do this:
$(".major .menu a").click(function(e){
$(".minor-result").removeClass("act");
$(".major-result").addClass("act");
e.preventDefault();
});
// if the parent is .minor, do this:
$(".minor .menu a").click(function(e){
$(".major-result").removeClass("act");
$(".minor-result").addClass("act");
e.preventDefault();
});
.page-body {margin:20px auto;width:200px;}
a {text-decoration:none;padding:5px 10px;display:inline-block;}
.make-minor a {background:blue;color:#fff;}
.make-major a {background:red;color:#fff;}
.major .menu a {color:orange;}
.minor .menu a {color:teal;}
.act {display:block !important;}
.major-result, .minor-result {display:none;}
.major-result a {color:red;}
.minor-result a {color:blue;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="page-body">
<div class="make-major"><a href="">make it Major</a></div>
<div class="make-minor"><a href="">make it minor</a></div>
<div id="scale" class="major"><!-- can be .minor -->
<ul id="wrap" class="menu">
<li id="one"><a href="">I</a></li>
<li id="two"><a href="">ii</a></li>
<li id="three"><a href="">III</a></li>
</ul>
</div>
<ul class="major-result act">
<li><a href="">One</a></li>
<li><a href="">Two</a></li>
<li><a href="">Three</a></li>
</ul>
<ul class="minor-result">
<li><a href="">one minor</a></li>
<li><a href="">two minor</a></li>
<li><a href="">three minor</a></li>
</ul>
</div>
The four
clicklisteners can be replaced with two:#scaleelement classes.actclass from result elements, builds the.ACTION_NAME-resultselector with current#scaleaction name, finds an element using that selector, and finally adds anactclass to it.General CSS classes
makeandresultwere added to simplify the script and styling.Five color actions have been added to demonstrate the seven different parent classes. All classes (including major, minor) are listed in
actionsvariable at the beginning of a script.