jQuery Page Widget How to - Check element text








Question

We would like to know how to check element text.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w ww .ja  v  a 2  s  .c o  m-->
    $("span.number").text(function (_, text) {
        if ("0" === text) {
            this.style.color = "red";
        }
    });
});
</script>
</head>
<body>
  <span class=number>0</span>
  <span class=number>50</span>
</body>
</html>

The code above is rendered as follows: