0

I know maybe couldn't do that Ex:

 <div class="dv1">
      <span correct="sp1"> D </span>
      <span correct="sp2"> C </span>
      <span correct="sp3"> Z </span>
 </div>

I can define like this :

$(".dv1 span[correct=sp2]").addClass("active");

Other way I want like this :

$(".dv1 span[text=C]").addClass("active");

So maybe there are other ways do the same right ? Thanks !

Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
tiepnv
  • 336
  • 2
  • 9
  • Possible duplicate of [jquery find element by text](http://stackoverflow.com/questions/7321896/jquery-find-element-by-text) – showdev May 10 '17 at 19:10

3 Answers3

1

Do it like this:

$('.dv1 span').filter(function(index) { return ($(this).text()).trim() === "D"? $(this) : false; });

var element = $('.dv1 span').filter(function(index) { return ($(this).text()).trim() === "D"? $(this) : false; });
element.addClass('active')
.active{
   color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dv1">
      <span correct="sp1"> D </span>
      <span correct="sp2"> C </span>
      <span correct="sp3"> Z </span>
 </div>
gaganshera
  • 2,629
  • 1
  • 14
  • 21
0

you can do this:

  $(".dv1 span").each(function(){
    if($(this).text().trim() == 'C'){
        $(this).addClass('active');
    }
  })
tech2017
  • 1,806
  • 1
  • 13
  • 15
0

I'm not sure the second way works. Here there is another way:

$(".dv1").find('span[correct=sp2]').addClass("active");

here is working.

Jose Rojas
  • 3,490
  • 3
  • 26
  • 40