If number is greater than 0 add class to span

How can I add a class to another class or id if the class/id contains a number greater than = or greater than 1? Below is my attempt.

$( document ).ready()
   if ($('#over') > 0){
     $('#over').addClass('readMe');
   }else{
     $('#over').addClass('free');
   }

I’m unsure of exactly what you mean, you want to check for a class like something-2 ?

var = myCondition = true;
$('#over').toggleClass('readMe', myCondition);

toggleClass might be what you’re after.

I want to check for a number and if the number is greater then 1, then addClass to the initial class.

0 <<< would have only one class called count

1 <<< would have two classes, the count class and the newClass

Sorry, that’s still not clear to me. What is an example of the class name with the numbers that you are trying to match?

<div id="over" class="something-0">I want a class "count"</div>
<div id="over" class="something-1">I want a class "count readMe"</div>
<div id="over" class="something-15">I want a class "count readMe"</div>

If you want to add a class based on the text content of that element, this can be done with .text() like

$('#over').addClass(function() {
    return parseInt($(this).text()) > 0 ? 'readMe' : 'free';
});
1 Like

That worked amazingly well @m3g4p0p thanks for your help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.