Abstracted Function for JavaScript Stripe Table Rows

Trying to figure out how to apply striping to table rows without directly referencing tag names or class names. The solution should be abstracted within the stripeRows() function in JavaScript, without using jQuery or innerHTML.

I'm almost there but struggling with a certain aspect of the file. Should I loop through the rows in the stripeMeTable() and webcoursesTable() functions or in the stripeRows(tableID,odd,even,over) function?

Below is my HTML and JavaScript code that needs to work seamlessly across both pages:

Sample HTML Code (page 1):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Striped Tables</title>
<link rel="stylesheet" type="text/css" href="striped-tables.css" />
<script type="text/javascript" src="striped-tables.js"></script>
</head>

<body>
<h1>Striped Tables</h1>
<table id="stripeme">
  <tr>
    <th>Food</th>
    <th>Rating</th>
  </tr>
  <tr class="lines">
    <td>Chocolate</td>
    <td>delicious</td>
  </tr>
  // more table rows...
</table>
</body>
</html>

More HTML (page 2):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Is your script abstracted?</title>
<link rel="stylesheet" type="text/css" href="abstraction.css" />
<script type="text/javascript" src="striped-tables.js"></script>
</head>

<body>
<h1>Another Striped Table</h1>
<table id="webcourses">
  <tr>
    <th>Course number</th>
    <th>Course content</th>
  </tr>
  // more table rows...
</table>
</body>
</html>

JavaScript File:

// JavaScript functions for striped tables

addLoadEvent(stripemeTable);
addLoadEvent(webcoursesTable);

function stripemeTable() {
    // Implementation details ...
}

function webcoursesTable() {
    // Implementation details ...
}

// End of JavaScript.

The challenge lies in looping through the table rows in a generic, abstracted way inside the stripeRows() function without explicitly referencing tag names:

function stripeRows(tableID, odd, even, over) {
    // Implementation details ...
}

Answer №1

Consider the following suggestions:

When looking at the function stripRows, note that the variable rows is referenced without being defined. It seems like you intended to include something like

var rows = document.getElementsByTagName('tr');
at the beginning of the function.

Additionally, within the same function, there is a line that reads

} else if (rows[i]/2 != 0 && !rows[0]) {
. It appears that instead of this, you might have meant } else if (i%2 == 0...){. In fact, it may be more effective to rewrite the entire conditional statement as follows:

if( i%2 == 0 ) {
    addClass(rows[i],'even');
} else {
    addClass(rows[i],'odd');
}

These adjustments should aid in addressing the errors present and set you on the right path towards resolving other similar issues in your code.

Answer №2

You do not require the use of getElementsByTagName. The table and table section elements have a property called rows, which represents their child rows. Therefore, you can implement something like this:

function stripeRows(tableSection, oddClass, evenClass) {
  var rows = tableSection.rows;

  for (var i=0, iLen=rows.length; i<iLen; i++) {
    rows[i].className = i%2? oddClass : evenClass;
  }
}

Please note that "tableSection" can refer to a table, tBody, tHead, or tFoot element. For simplicity in this example, I have used an "addClass" function to add the class.

Edit

A few comments on the code:

function stripemeTable() {
    // obtain the <tr> tags:
    var stripemeRows = document.getElementsByTagName('tr');

This fetches all tr elements within the document, but it might be more efficient to do this table by table.

// IF the tableID is NOT stripeme, exit the stripemeTable function:
if (!document.getElementById || !document.getElementsByTagName || !document.getElementById('stripeme'))  {
    return false;

In this snippet, there is a check for getElementsByTagName, although this method has already been utilized in the preceding line. If testing for getElementById is necessary (which is uncommon now that most browsers support it), consider:

     var stripeMe = document && document.getElementById && document.getElementById('stripeme');
     if (stripeMe) {

The assumption here is that the rows belong to the table 'stripeMe' without confirming it. If striping the rows only when they are within a table element with an ID of 'stripeMe', then proceed accordingly. It would be preferable to use a class instead. The else statement becomes unnecessary, simply continue as follows:

// ELSE loop through the rows and apply the stripeRows function:  
} else {
    for (var i = 0; i < stripemeRows.length; i++) {
        stripeRows('stripeme','pri-stripe','sec-stripe','over-stripe');
    } // end of the rows FOR loop 
} // end of the tableID test IF/ELSE

In place of this approach:

     var stripemeRows = stripeMe.rows;
     for (var .... ) {
       ...
     }

If you specifically want to stripe the rows inside the body, separate the header rows into a tHead element and stripe only the rows within the tbody, for example:

 var stripemeRows = stripeMe.tBodies[0].rows;

Answer №3

Hello @Jason-McCoy, after reviewing the requirements thoroughly, I am confident that this solution fulfills all the criteria you have set. By starting the initialization of i at 1 instead of zero, the first line is preserved. Additionally, the stripeRows function does not perform any direct selections based on the elements passed to it. The striping functionality should work properly with this code, although some bugs still remain in the onmouseover section. Please pay close attention to that during the implementation.

addLoadEvent(stripeme);

function stripeme() {
    if (!document.getElementById || !document.getElementsByTagName) { 
        return false;
    }

    // If the tableID is NOT "webcourses", exit the webcoursesTable function:
    if (!document.getElementById('webcourses') && !document.getElementById('stripeme')) {
        return false;
    }

    // Retrieve the <tr> tags:
    var rows = document.getElementsByTagName('tr');

    for (var i=1; i < rows.length; i++) {
        stripeRows(rows[i],(i%2==0)?'pri-stripe':'sec-stripe','over-stripe');
    } // end of the rows FOR loop 
} // end of the stripemeTable function

function stripeRows(row,rowClass,over) {
    addClass(row,rowClass);

    // Get the current className of the table rows:
    var oldClassName = row.className;

    // Handle the onmouseover event for the table rows:
    mouseOver = row.onmouseover;
    mouseOver = function() {
        addClass(row,over);
    } // End of the onmouseover anonymous function

    // Handle the onmouseout event for the table rows:
    mouseOut = row.onmouseout;
    mouseOut = function() {
        addClass(row,oldClassName);
    } // End of the onmouseout anonymous function
} // End of the stripeRows function 

function addClass(element,theClass) {
    // If the table row does not have a className, append the required className:
    if (!element.className) {
        element.className = theClass;
    // Else append the required className to the className(s) that are already assigned to that row:
    } else {
        var newClassName = element.className;
        newClassName += " ";
        newClassName += theClass;
        element.className = newClassName;
    } // End of the className IF/ELSE  
} // End of the addClass function

function addLoadEvent(func) {
    // Assign the window.onload function to a variable:
    var oldonload = window.onload;

    // If the window.onload does not call a function:
    if (typeof window.onload != 'function') {
        window.onload = func;
    // Else set the window.load:
    } else {
        window.onload = function() {
            oldonload();
            func();
        } // End of the oldonload anonymous function
    } // End of the typeof IF
} // End of the addLoadEvent function

For your CSS styling:
You can either include this in both striped-tables.css and abstraction.css, or link to the same CSS file in both HTML files.

.pri-stripe {
    background-color: green;
}

.sec-stripe {
    background-color: blue;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Specify the data type of a nested object in a React component with TypeScript

Interface Button{ buttonTitle: { name?: string; } } What is the best way to specify a type for the buttonTitle property? ...

The issue I am facing currently revolves around the absence of images on

I recently uploaded a captivating HTML page for my website's captive portal. Although the redirection to the designated page is working perfectly, I am facing an issue with the images included in the HTML page. Even though I went ahead and uploaded th ...

Unexpectedly, Ajax call is triggering additional callbacks

I am currently facing an issue with my AJAX request in the code below. The Chrome Inspector is showing that the callback function associated with the request is being called twice, resulting in the response being logged into the console twice. Additional ...

405 (Method Not Allowed) using the concept of notion

Currently, I am heavily involved in a compact project where data submitted through a form will be seamlessly posted on Notion. An error occurred while attempting to submit the form: contact.jsx?287f:14 POST http://localhost:3000/contact 405 (Meth ...

Using the ng-repeat directive in an angular-nvd3 tooltip

Looking to dynamically repeat values in the tooltip content of an nvd3 graph? Not sure how to achieve this using ng-repeat with JSON data? Seeking guidance on alternative methods? I can help! $scope.options = { chart: { type: ...

Tips for integrating SASS from the Bulma package into an Angular application

I'm currently working on implementing Bulma into my project. The documentation suggests using NPM to obtain it. yarn add bulma Further in the documentation, there is an example provided with code snippets like the ones below. <link rel="styles ...

Can a substring within a string be customized by changing its color or converting it into a different HTML tag when it is defined as a string property?

Let's discuss a scenario where we have a React component that takes a string as a prop: interface MyProps { myInput: string; } export function MyComponent({ myInput }: MyProps) { ... return ( <div> {myInput} </div> ...

Modifying column layout within an HTML webpage

I'm seeking advice on modifying a code. I'd like to keep it the same as it is now, but with one change - I want to decrease the width of the main video box and add another one beside it with an iframe code for a chatbox. Here's the current c ...

What is the best way to center align the div container horizontally?

Trying to center the #container but all methods have failed. How can I achieve centering using only CSS? body { text-align:center; } <div id="app"> <div id="container"><div id="container_red" style="position:absolute;left:0"> ...

Performing a mass update in MongoDB with the help of mongoose

Is there a way to perform bulk upserts with Mongoose? Essentially, I want to have an array and insert each element if it does not exist, or update it if it does. (I am using custom _ids). When I try using .insert, MongoDB throws an error E11000 for duplic ...

Creating nested columns in a bootstrap grid system can enhance the structure and organization of your

I am currently attempting to use Bootstrap grid to create nested columns, but unfortunately, the layout I am getting is not nested as intended. {% for home in home %} <div class="container"> <div class="row" ...

The function createVNode does not exist in the context of Vue integrated with Laravel

I've been attempting to replicate a component rendering based on an online example. While it works smoothly in a sample project, it crashes when applied to the official codebase. Here is the content of the blade file being rendered: <html lang=&q ...

PHP not displaying radio button values

My latest project involved creating a quiz program using only PHP. The program consists of 4 different questions and radio buttons, and when an option is selected, the value of the radio button (A, B, C, or D) should be printed. I have set up 4 PHP varia ...

What are some effective methods for locating dual edges within a Half-Edge (DCEL) data structure?

I have implemented a HalfEdge data structure to represent the connectivity between faces in my mesh. While importing an external model, I construct the HalfEdge structure. However, for meshes with numerous triangles, the construction process is time-consu ...

Retrieve the HTML content from a separate frame located on a different domain

Is it possible to access the HTML content of a frame located on a different domain using JavaScript? I understand that this restriction is in place for security reasons, but my intention is legitimate. Best regards, Peter ...

Click on the canvas to fill a specific area with JavaScript

My goal is to implement a JavaScript function that will shade specific areas of canvas drawings when clicked. Below is the code I have that draws a square inside a circle. <!DOCTYPE HTML> <html> <head> </head> <body&g ...

What is the best way to set up Paypal payments to automatically send out confirmation emails that are already in

Once the payment is processed, the website will send you your details. I am familiar with adding a paypal button, but how do I connect it to my website to automatically trigger the confirmation email after the payment has been completed through paypal? ...

`Is it possible to animate the rotation of an image within an input control?`

After coming across this helpful answer, I successfully added a rotating GIF image as an icon inside an INPUT control on its right side. Simply applying the "busy" class to my control did the trick, and now it looks like it's in progress. input.busy { ...

Incorporating React-Native components into a Next.js application within an Nx monorepository: A Step-by-Step

I'm encountering an issue while attempting to integrate React Native components into an Nx monorepo setup. Initially, the Nextjs app compiles successfully: info - Fast Refresh enabled for 1 custom loader event - client and server compiled successful ...

Internet Explorer and JavaScript are two important components of web

I'm encountering an issue with my registration page where it functions properly in Firefox, but IE 6-8 displays a 'document.info.altDate1.value' error upon submission. Here is the structure of the body: <DIV id=wrapper> <DIV id=ou ...