I've been struggling with this simple script and could not find related problem here in StackOverflow. Here is my problem.
I have two boxes. A box is highlighted with blue if <div class="notselected">
becomes <div class="selected">
thru click event.
If no box is selected, the parent box should be highlighted with red, like so:
Here is a snippet:
$(document).ready(function(){
var parent = $(this).find(".parent");
if(parent) {
var selected = parent.find("div.selected");
if(selected) {
selected.css({"color": "blue", "border": "2px solid blue"});
}
else {
parent.css({"color": "red", "border": "2px solid red"});
}
}
});
.ancestors *:not(script) {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body class="ancestors">
<div class="parent"> Parent Box
<div class="notselected"> Box A </div>
<div class="notselected"> Box B </div>
</div>
</body>
Please let me know what I am missing. Thanks for any help!