Search code examples
jqueryhtmlclassjsrenderjsviews

How to add a class to a div at one condition?


I have a number, a, which gets at each 3 secs a random value from 0 to 100.

            <div class="industrial gauge size two warning">
                <span class="ticks" data-amount="8" data-scale-freq="1"></span>
                <div class="space"></div>
                <div class="meter"></div>
            </div>

For example when a>80 I wanna add a class, named "danger", to the first div. How could I do this?


Solution

  • Give the div an id for example

    <div id="first" class="space"></div>
    

    and use this code at Jquery

       if(a>80)
    {
        $("#first").addClass('danger');
    }
    

    Or I recommend for you to write as many CSS classes as you want and change the attribute class in any condition you like

       if(a>80)
    {
        $("#first").attr('class', 'newClass');
    }