Michael Sandusky Michael Sandusky - 4 months ago 7
CSS Question

Change All Values in HTMLDIVS if Inner Value is a Negative Number

We have a PHP SQL Query that is returning an JSON Array of values which are being assigned to DIVS in the HTML.
The multiple returned values look something like this for the end result:

<div id = "tdiv"> -58 </div>
<div id = "tdiv2"> -85 </div>
<div id = "tdiv3"> 58 </div>


We are attempting to assign a red color to all DIV values that have a Negative number.

Any Ideas?

Thanks Guy

Answer

Use jQuery.filter over Attribute starts with selector

$('[id^=tdiv]').filter(function() {
  return this.textContent < 0;
}).css('color', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="tdiv">-58</div>
<div id="tdiv2">-85</div>
<div id="tdiv3">58</div>

Comments