user2557504 user2557504 - 1 month ago 8
CSS Question

check for number then replace

I have an issue. I am checking a specific span class for a number, if the number equals a specific amount- I want to simply swap the number for either a icon or image. I can get the alert to trigger if the number matches, but how do I replace the number for the heart or an image as well as removing the best replies text? and aside from jquery can this be solved with css alone?

Thanks for your help!



var span;
$('span').each(function(){
if($(this).html() == '199'){
span = $(this);
alert("this");
//$(this).html() == '♥');
}
});

<li id="myMemberStatsBestResponses" class="ms-comm-myMemberStatsItem"><span class="ms-comm-reputationNumbers">199</span> best replies</li>




Answer

Use the html function to set the content:

  $(this).html('♥');

jQuery's html function accept as parameter the html value to set into the element that was selected.

Since you want to replace both the text of the span and the text afterward (which mean - you actually wants to replace the content of the parent li element, you can use this:

var span;
$('span').each(function(){
  if($(this).html() == '199'){
    $(this).parent('li').html('♥');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="myMemberStatsBestResponses" class="ms-comm-myMemberStatsItem"><span class="ms-comm-reputationNumbers">199</span> best replies</li>

Comments