jQuery select elements that didn't pass a function test

Description

jQuery select elements that didn't pass a function test

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selecting Elements that Didn't Pass a Function Test in jQuery</title>
<style>
    .highlight{//from  w  ww . j  a  va 2 s . c  o  m
        background: yellow;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $("ul li").not(function(index){
        return index % 2 !== 0;
    }).addClass("highlight");
});
</script>
</head>
<body>
  <h2>Unordered List</h2>
    <ul>
        <li>First list item</li>
        <li>Second list item</li>
        <li>Third list item</li>
        <li>Fourth list item</li>
    </ul>
  <hr>
  <h2>Another Unordered List</h2>
  <ul>
        <li>First list item</li>
        <li>Second list item</li>
        <li>Third list item</li>
        <li>Fourth list item</li>
    </ul>
</body>
</html>



PreviousNext

Related