6

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.

enter image description here

If no box is selected, the parent box should be highlighted with red, like so:

enter image description here

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!

Zenoo
  • 12,670
  • 4
  • 45
  • 69
user1506104
  • 6,554
  • 4
  • 71
  • 89

2 Answers2

4

You were very close!

Simply check for selected.length instead of selected.

Demo for parent selection

$(document).ready(function() {
  var parent = $(this).find(".parent");
  if (parent) {
    var selected = parent.find("div.selected");
    if (selected.length) {
      selected.css({
        "color": "blue",
        "border": "2px solid blue"
      });
    } else {
      parent.css({
        "color": "red",
        "border": "2px solid red"
      });
    }
  }
});
.ancestors * {
  display: block;
  border: 2px solid lightgrey;
  color: lightgrey;
  padding: 5px;
  margin: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
</script>

<div class="parent"> Parent Box
  <div class="noselected"> Box A </div>
  <div class="notselected"> Box B </div>
</div>

Demo for child selection

$(document).ready(function() {
  var parent = $(this).find(".parent");
  if (parent) {
    var selected = parent.find("div.selected");
    if (selected.length) {
      selected.css({
        "color": "blue",
        "border": "2px solid blue"
      });
    } else {
      parent.css({
        "color": "red",
        "border": "2px solid red"
      });
    }
  }
});
.ancestors * {
      display: block;
      border: 2px solid lightgrey;
      color: lightgrey;
      padding: 5px;
      margin: 15px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  <script>
  </script>

  <div class="parent"> Parent Box
    <div class="selected"> Box A </div>
    <div class="notselected"> Box B </div>
  </div>
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

$(document).ready(function() {
  var parent = $(this).find(".parent");
  // check for the length property of the jquery object to get the number of elements matched
  if (parent.length != 0) {
    var selected = parent.find("div.selected");
    // same goes here
    if (selected.length != 0) {
      selected.css({
        "color": "blue",
        "border": "2px solid blue"
      });
    } else {
      parent.css({
        "color": "red",
        "border": "2px solid red"
      });
    }
  }
});
.ancestors * {
  display: block;
  border: 2px solid lightgrey;
  color: lightgrey;
  padding: 5px;
  margin: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="ancestors">
  <div class="parent"> Parent Box
    <div class="notselected"> Box A </div>
    <div class="notselected"> Box B </div>
  </div>
</div>
davidchoo12
  • 1,261
  • 15
  • 26