Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Zebra Striping in HTML Tables with jQuery and Vanilla JavaScript

Tech Apr 26 12

How to Include jQuery?

To demonstrate the convenience of jQuery, we'll implement a zebra striping effect on an HTML table using three different approaches: static CSS classes, vanilla JavaScript, and jQuery. This comparison highlights jQuery's simplicity and efficiency.

Static CSS Class Implementation

First, we can achieve zebra striping by directly applying CSS classes to table rows in the HTML. This method is straightforward but lacks dynamic flexibility.

Vanilla JavaScript Implementation

Next, we use vanilla JavaScript to dynamically apply zebra striping. This approach involves more code but allows for runtime modifications.


<html>
    <head>
        <meta charset="utf-8">
        <title>Zebra Striping with Vanilla JavaScript</title>
        <style type="text/css">
            .header-row {
                background-color: greenyellow;
            }
            .odd-row {
                background-color: yellowgreen;
            }
            .even-row {
                background-color: yellow;
            }
        </style>
        <script type="text/javascript">
            window.onload = function() {
                const tableRows = document.getElementsByTagName("tr");
                for (let i = 0; i < tableRows.length; i++) {
                    if (i === 0) {
                        tableRows[i].className = "header-row";
                    } else if (i % 2 === 1) {
                        tableRows[i].className = "odd-row";
                    } else {
                        tableRows[i].className = "even-row";
                    }
                }
            }
        </script>
    </head>
    <body>
        | Name | Class | Birthday | Constellation | Mobile |
|---|---|---|---|---|
| isaac | W13 | Jun 24th | Cancer | 1118159 |
| fresheggs | W610 | Nov 5th | Scorpio | 1038818 |
| girlwing | W210 | Sep 16th | Virgo | 1307994 |
| tastestory | W15 | Nov 29th | Sagittarius | 1095245 |
    </body>
</html>

jQuery Implementation

Now, we implement the same effect using jQuery, which significantly reduces the code complexity.


<html>
    <head>
        <meta charset="utf-8">
        <title>Zebra Striping with jQuery</title>
        <!-- Include jQuery library -->
        <script src="js/jquery.min.js"></script>
        <script>
            $(function() {
                $("tr:odd").css("backgroundColor", "yellowgreen");
                $("tr:even").css("backgroundColor", "lightyellow");
                $("tr:first").css("backgroundColor", "greenyellow");
            });
        </script>
    </head>
    <body>
        | Name | Class | Birthday | Constellation | Mobile |
|---|---|---|---|---|
| isaac | W13 | Jun 24th | Cancer | 1118159 |
| fresheggs | W610 | Nov 5th | Scorpio | 1038818 |
| girlwing | W210 | Sep 16th | Virgo | 1307994 |
| tastestory | W15 | Nov 29th | Sagittarius | 1095245 |
    </body>
</html>

This example demonstrates that jQuery simplifies JavaScript code, aligning with its "write less, do more" philosophy.

jQuery Page Load Function

The page load funcsion in jQuery is crucial for binding events after the document is ready. Its widely used in various applications.


<html>
    <head>
        <meta charset="UTF-8">
        <title>jQuery Page Load Function</title>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script>
            // Long form: jQuery(document).ready(function() { ... });
            // Short form: $(document).ready(function() { ... });
            // Common shorthand: $(function() { ... });

            $(function() {
                const spanElement = document.getElementById("textSpan");
                console.log(spanElement.innerText);
            });

            $(function() {
                console.log("Second page load function");
            });

            $(function() {
                console.log("Third page load function");
            });
        </script>
    </head>
    <body>
        <span id="textSpan">Sample text</span>
    </body>
</html>

Summary

To use jQuery, first include the jQuery JavaScript file. jQuery itself is a JS library, with jquery.min.js being the minified version recommended for production. The $ symbol is central to jQuery and serves two primary purposes in this context:

  1. Page Load Function: jQuery(document).ready(function() {}); can be shortened to $(document).ready(function() {}); or further to $(function() {});. This is similar to window.onload = function() {} but more powerful. Unlike window.onload, which can only be defined once per page and waits for all resources (images, links, media) to load, $() allows multiple non-conflicting definitions and executes earlier.
  2. Selector Indicator: $(selector) is used for element selection. jQuery offers robust selector capabilities, which will be covered in detail in subsequent sections.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.