0

I have a few elements with the same class name data for example, they could look like this:

<p class="data">AAA</p>
<p class="data">BBB</p>
<p class="data">CCC</p>
<p class="data">DDD</p>

Now I want to loop through all the elements with class name data and see if any of them match a string, say AAA. If it does, then make that element red color.

$(".data").each(function() {
    if (this.val() == "AAA") {
        $(this).css("color", "red");
    }
});

However, this isn't working. Any ideas?

0

6 Answers 6

2

this should be $(this) and val() should be html(). val() is used for elements with a value attribute like input fields and radio buttons ect. html() gets the html inside an element. this is the vanilla Javascripts version of jQuerys $(this).

$(document).ready(function(){
    $(".data").each(function() {
        if ($(this).html() == "AAA") {
            $(this).css("color", "red");
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

No need to loop through the class name to achieve that result. You can simply write

$('.data:contains("AAA")').css('background-color', 'red');

Note: This will only work if you are sure enough that other tags won't contain AAA. For example in following scenario it may not work.

<p class="data">AAA BBB</p>
<p class="data">AAA</p>
<p class="data">CCC</p>
<p class="data">DDD</p>

1 Comment

"it may not work" is unclear. Rather say that the element having AAA BBB will be targeted - since it actually contains "...AAA..."
1

I'd suggest:

// Select all '.data' elements, and filter that collection
// Using jQuery's filter() method:
$(".data").filter(

  // using an arrow function to retain only those
  // elements whose trimmed text-content is
  // equal to "AAA":
  (index, element) => $(element).text().trim() === "AAA"

// using the css() methods to set the color property to
// To 'red':
).css('color', 'red');

1 Comment

I can't believe I haven't heard of or used an arrow function before. Thanks for prompting me to look up this syntax!
1

Using filter()

$(".data").filter(function(i, el){
  return $(el).text() === "AAA"; 
}).css({color: "red"});
<p class="data">AAA</p>
<p class="data">BBB</p>
<p class="data">CCC</p>
<p class="data">DDD</p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Using .text()

$(".data").text(function(i, txt){
  if(txt==="AAA") $(this).css({color: "red"});
});
<p class="data">AAA</p>
<p class="data">BBB</p>
<p class="data">CCC</p>
<p class="data">DDD</p>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

p tag does not have any value, it has text and $(this) should be used to apply jquery functions, only this will not work.

$(".data").each(function() {
    if ($(this).text() == "AAA") {
        $(this).css("color", "red");
    }
});

Comments

0

use $(this) instead this

$(".data").each(function() { 
  if ($(this).html() == "AAA") { 
    $(this).css("color", "red"); 
  } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<p class="data">AAA</p>
<p class="data">BBB</p>
<p class="data">CCC</p>
<p class="data">DDD</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.