0

I'm trying to create a lille script to go trough my page and check if a class is present in any <div>. If the class is present then there will be no action.

It will look for class="blue" and if it find it it will do nothing. If it doesn't fint class="blue" it will change background color for class="yellow". What it does it change the background color for class="yellow" not matter what. What is wrong?

$("div").each(function() {
  if (jQuery(this).attr('class') != undefined && jQuery(this).hasClass('blue')) {} else {
    $('.yellow').css('background', 'green')
  }
});
.blue {
  background: blue;
  width: 100px;
  height: 100px;
}

.red {
  background: red;
  width: 100px;
  height: 100px;
}

.yellow {
  background: yellow;
  width: 100px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="blue">
  Blue
</div>

<div class="red">
  Red
</div>

<div class="yellow">
  Yellow
</div>

6
  • 1
    I'm trying to wrap my head around this one. Hold on... Commented Sep 20, 2018 at 20:50
  • $('.yellow').css('background', 'green'); would get the same result. Commented Sep 20, 2018 at 20:51
  • Ok, I think I've figured it out... Please wait... Commented Sep 20, 2018 at 20:54
  • jQuery(this).attr('class') != undefined <-- why are you doing this? Commented Sep 20, 2018 at 20:58
  • Your code is saying, hey the first div has the class blue.... next one.... hey the class is not blue.... Commented Sep 20, 2018 at 21:00

2 Answers 2

2

One liner... :)

http://jsfiddle.net/yak613/bcgajp6q/

Basically, you want that if a blue is there, the .yellow is green. Otherwise, it stays yellow.

You can see what happens when the blue is there by going to the fiddle (^above) and uncommenting the blue.

if(!$(".blue").length) $(".yellow").css('background-color', 'green');
.blue {
  background: blue;
  width: 100px;
  height: 100px;
}

.red {
  background: red;
  width: 100px;
  height: 100px;
}

.yellow {
  background: yellow;
  width: 100px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- <div class="blue">
  Blue
</div> -->
<div class="red">
  Red
</div>

<div class="yellow">
  Yellow
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

I just need it the other way around but I like your approach. This finds the blue and returns green. I need it to check that blue doesn't exist and then change the value on the other css-class.
So if blue doesn't exist, then change the .yellow? Ok
I've got it! Thanks a lot! The ! was the missing part! I will approach with your script :)
You mean: "Basically, you want that if blue is NOT there, the .yellow is green."
0

All you need to do is simply check if the target element doesn't have the .blue class (with if (!$(this).hasClass('blue'))).

This can be seen in the following:

$("div").each(function() {
  if (!$(this).hasClass('blue')) {
    $('.yellow').css('background', 'green')
  }
});
.blue {
  background: blue;
  width: 100px;
  height: 100px;
}

.red {
  background: red;
  width: 100px;
  height: 100px;
}

.yellow {
  background: yellow;
  width: 100px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="blue">
  Blue
</div>

<div class="red">
  Red
</div>

<div class="yellow">
  Yellow
</div>

1 Comment

This does exactly what my own script does :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.