Adding a Sequence of Class Names to <li> Elements with JavaScript

How can you easily generate sequential class names in a for loop?

Answer №1

To add the iteration plus 1 from your for loops in the class being added, you can use this example:

for(var i = 0; i < frets; i++){
  var fretRow = document.createElement("ul");
  fretRow.classList.add("fret-row-" + (i + 1));
  
  // Create li items for each string and add them to the fretRow
  for(var j = 0; j < strings; j++){
    var fret = document.createElement("li");
    fret.classList.add("string-" + (j + 1));

    fretRow.appendChild(fret);
  }

  // Add the entire fretRow to the fretboard div
  fretboard.appendChild(fretRow);
}

Answer №2

Follow the instructions to create a fretboard using JavaScript:

for(var x = 0; x < totalFrets; x++){
  var fretRowElement = document.createElement("ul");
  fretRowElement.classList.add("fret-row" + (x+1));
  for(var y = 0; y < totalStrings; y++){
    var stringElement = document.createElement("li");
    stringElement.classList.add("string" + (y+1));
    fretRowElement.appendChild(stringElement);
  }
  guitarFretboard.appendChild(fretRowElement);
}

Answer №3

To achieve this, modify your for loop as follows: Replace

fretRow.classList.add("fret-row")
with
fretRow.classList.add("fret-row", "fret-row"+(i+1));
, and replace fret.classList.add("string") with
fret.classList.add("string"+(j+1))

for(var i = 0; i < frets; i++){
   var fretRow = document.createElement("ul");
   fretRow.classList.add("fret-row", "fret-row"+(i+1));

   for(var j = 0; j < strings; j++){
        var fret = document.createElement("li");
        fret.classList.add("string"+(j+1));
        fretRow.appendChild(fret);
   }

   fretboard.appendChild(fretRow);
}

VIEW CODE PEN DEMO

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

What steps can I take to create a responsive jQuery UI buttonset?

I am currently facing a challenge with my web app (http://www.tntech.edu/cafe-menu.php) which is being iframed into a mobile application developed by ATT. The button sizes vary on different phone models, and I am seeking a solution to make them responsiv ...

What is the reason for the absence of the $.ajax function in the jQuery package designed for node.js?

Here is my code sample, which I would like to use for practicing with jQuery's ajax function. Note that I have already installed the jQuery package using npm install jquery: var $ = require('jquery'); var remoteValue = false; var doSometh ...

Troubleshooting problem in Vercel with Next.js 13 application - encountering "Module not found" error

Encountering a deployment issue with my Next.js 13 application on Vercel. I recently implemented the Parallel Routes feature of Next in my codebase. While pushing the changes to create a new pull request on GitHub, the branch's deployment on Vercel is ...

Currently trapped within the confines of a Next.js 13 application directory, grappling with the implementation of a

I need to figure out how to export a variable from one component to layout.tsx in such a way that it is not exported as a function, which is currently causing the conditional check in the class name to always be true. Below is the code snippet: // File w ...

Passing data into a different controller

In the process of building a system that involves selecting elements from a list and viewing their details, I have encountered an issue while using MEAN stack. My goal is to pass the id of the selected element to the controller of the second page. However, ...

Creating an Art Deco inspired border using CSS

I am interested in creating a border effect using only CSS. Here is an image of the effect I am looking to achieve: https://i.sstatic.net/cKWXZm.jpg I would like to achieve this effect without adding additional div elements. Any suggestions or tips on how ...

What is the best way to notify the JSON code below using jQuery?

I have received a JSON response after using the Reverse Geocoding API from Google. The response includes various address components such as route, sublocality, locality, and political information. { "results": [ { "address_components": [ ...

An error was encountered: SyntaxError - An unexpected token '!' was found

I am having trouble creating a react cluster map. I encountered a SyntaxError, and I'm not sure what went wrong. Initially, my map was working fine, but after using the use-supercluster npm package, it started showing an Uncaught SyntaxError: Unexpect ...

What could be causing my website to have a horizontal scroll even when the width is set to 100%?

My website seems to be scrolling horizontally, possibly due to an issue with the footer. I suspect it might be related to the social media icons in the footer, but I'm not entirely sure. Any guidance on resolving this problem would be greatly apprecia ...

Initiate a file download following the redirection of a user to a new page in NodeJS/Express

Overview I am currently developing a NodeJS project using Express. In the application, there are buttons visible to the public that trigger file downloads. These buttons are linked to protected routes which will redirect users to a login page if they are ...

Modify text using JQuery when the span is clicked

Currently, I am attempting to retrieve a value from the database: SenderDriver->total_trips. Everything seems fine, but I have a specific id that needs to be placed within onClick(), which then sets the value of the database variable: SenderDriver-> ...

Vue.js computed property experiencing a minor setback

I'm currently working on developing a test-taking system using Vue and Laravel. When a user inputs the test code and email address, they are directed to the test page. To display all the test questions based on the entered code, I implemented a naviga ...

Using jQuery to modify text color

I'm new to learning jQuery and I want to make a text color change on my website when the user scrolls. The piece of text is inside a div with the id #headerTitle in my CSS file. This is the current code I have: $(document).ready(function(){ $(w ...

Desiring the standard back button feature of a browser to work seamlessly when loading pages via ajax requests

I am currently developing a web application in PHP and implementing the use of AJAX requests to retrieve pages from the server. For example, when a user clicks on menu tabs, an AJAX request is made to the server to fetch the page and load it into a specifi ...

align all items centrally and customize Excel columns based on the length of the data

Is there a way to dynamically adjust the column width based on the length of data in an Excel report using PHPexcel? Additionally, how can I center all the data in the Excel sheet? Here is the current code snippet: <?php if (!isset($_POST['send&a ...

Having trouble retrieving AJAX response data using jQuery

I have been searching and attempting for hours without success. On my current page, I am displaying basic data from a database using PHP in an HTML table. However, I now want to incorporate AJAX functionality to refresh the data without reloading the page ...

Utilizing BootStrap 4 to ensure uniform image sizes within a row of columns

I'm facing a challenge in creating a row of 4 images that are responsive and uniform in size, despite being different dimensions (640x799, 640x479, ...). I've attempted to use columns and img-fluid to achieve this, but the shorter images do not f ...

Ways to integrate npm dependencies into your Cordova plugin

Currently working on implementing a Cordova plugin called core-cordova found in this repository. This particular plugin has a dependency on another NPM package. The issue arises after installing the plugin in my app using: $ cordova plugin add @aerogears ...

Alter the value of a parameter within a script tag with JavaScript/jQuery

Looking to dynamically change a parameter value using JavaScript or jQuery. Here is an example URL: www.exampleurl.com/somepage?foo=test I have a function that can extract the value after the 'foo' parameter: function getParameterByName(name, ...

Send multiple values as arguments to a jQuery function

Beginner question ahead: I'm using the jquery function loadNewPicture() to upload pictures and the progress() function to track the percentage of the upload. Everything is functioning correctly. My query relates to the variables that can be passed t ...