Steps for incorporating the getElementByClassName() method

I have developed an application that features a list displayed as shown below:

https://i.stack.imgur.com/BxWF2.png

Upon clicking each tick mark, the corresponding Book name is added to a textbox below. I desire the tick mark to be replaced by a cross symbol which allows users to remove it.

Currently, when I click on any of the tick marks, only the first item in the list changes to a cross like this:

https://i.stack.imgur.com/tftkk.png

However, I want the tick next to the specific book to change into a cross, not just the first one at the top of the list.

I understand that this issue stems from the need for the items to be treated as class elements rather than ID tags, as using an ID tag always selects the first item with that particular ID. However, I am unsure about how to incorporate this change into my code even after attempting to wrap the tick and cross icons in divs with class names.

The JavaScript code involved looks like this:

function saveToList(event) {
    if (event.which == 13 || event.keyCode == 13) { 
    
function saveToFB(bookName) {
  
var user = firebase.auth().currentUser;
var bookList = firebase.database().ref('users/' + uid + '/');
var uid = user.uid;

// This will save data to Firebase
bookList.push({
    book: bookName
    });
};

// This part contains JS responsible for creating the lists based on Firebase data, divided into three lists each holding up to 10 books.
function refreshUI(list) {
var lis = '';
var lis2 = '';
var lis3 = '';

for (var i = 0; i < 10 && i < list.length; i++) {

// Creates the list item by adding the firebase object + genLinks which contains the select, remove, and delete icons. 
lis += '<li data-key="' + list[i].key + '">' + list[i].book + genLinks(list[i].key, list[i].book) +'</li>';
      };
      
for (var i = 10; i < 20 && i < list.length; i++) {
      
lis2 += '<li data-key="' + list[i].key + '">' + list[i].book + genLinks(list[i].key, list[i].book) +'</li>';
      };
      
for (var i = 20; i < 30 && i < list.length; i++) {
      
 lis3 += '<li data-key="' + list[i].key + '">' + list[i].book + genLinks(list[i].key, list[i].book) +'</li>';
      };
      
      // Populates the HTML lists with the JS list items
        document.getElementById('bookList').innerHTML = lis;
        document.getElementById('bookList2').innerHTML = lis2;
        document.getElementById('bookList3').innerHTML = lis3;
    };

...

A helpful suggestion was made to use getElementsByClassName(), however, no explanation was provided on how to implement it. If anyone could offer some guidance or advice on this matter, I would greatly appreciate it.

Thank you, G

Answer №1

Styling the icon display through CSS is a convenient method, especially when using checkboxes for selection indication. Retrieving the checked items becomes easy with a simple query and loop process.

function createList() {
   /* Collect checked checkboxes */
   var checkboxes = document.querySelectorAll('[name="books"]:checked');
   /* Iterate over elements to build value list */
   var out = [];    
   for (var i=0; i<checkboxes.length; i++) {
       out.push(checkboxes[i].value);       
   }
   /* Join values into a list and show them */
   document.getElementById("output").innerHTML = out.join(", ")
}
document.getElementById("booklist").addEventListener("change", createList);
createList();
[name="books"] {  /* Hide checkbox */
  display: none
}

[name="books"]+i+i { /* Hide the x when not checked */
  display: none
}

[name="books"]:checked+i {  /* Hide the check when checked */
  display: none
}

[name="books"]:checked+i+i { /* show the x when checked */
  display: inline
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<ul id="booklist">
  <li>
    <label>
      <span>Book 1</span>
      <input type="checkbox" value="book1" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 2</span>
      <input type="checkbox" value="book2" name="books" checked/>
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 3</span>
      <input type="checkbox" value="book3" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 4</span>
      <input type="checkbox" value="book4" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
</ul>

<div id="output"></div>

To accommodate multiple lists, slight adjustments can be made. Utilizing data attributes to specify item outputs and applying event listeners to parent elements iteratively streamlines the handling of various lists.

(function() {
  function createList(listId) {
    /* Obtain checked checkboxes */
    var checkboxes = document.querySelectorAll('#' + listId + ' [name="books"]:checked');
    /* Gather values into a list by looping through elements */
    var out = [];
    for (var i = 0; i < checkboxes.length; i++) {
      out.push(checkboxes[i].value);
    }
    var outputId = document.getElementById(listId).dataset.output;
    /* Merge list and display it */
    document.getElementById(outputId).innerHTML = out.join(", ")
  }


  var lists = document.querySelectorAll(".book-list");
  for (var i = 0; i < lists.length; i++) {
    (function(list) {
      list.addEventListener("change", function() {
        createList(list.id)
      });
      createList(list.id);
    }(lists[i]))
  }
}());
[name="books"] {
  /* Hide checkbox */
  display: none
}

[name="books"]+i+i {
  /* Hide the x when not checked */
  display: none
}

[name="books"]:checked+i {
  /* Hide the check when checked */
  display: none
}

[name="books"]:checked+i+i {
  /* show the x when checked */
  display: inline
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<ul id="booklist1" class="book-list" data-output="output1">
  <li>
    <label>
      <span>Book 1</span>
      <input type="checkbox" value="book1" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 2</span>
      <input type="checkbox" value="book2" name="books" checked/>
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 3</span>
      <input type="checkbox" value="book3" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 4</span>
      <input type="checkbox" value="book4" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
</ul>

<div id="output1"></div>

<ul id="booklist2" class="book-list" data-output="output2">
  <li>
    <label>
      <span>Book 5</span>
      <input type="checkbox" value="book5" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 6</span>
      <input type="checkbox" value="book6" name="books"/>
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 7</span>
      <input type="checkbox" value="book7" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
  <li>
    <label>
      <span>Book 8</span>
      <input type="checkbox" value="book8" name="books" />
      <i class="fa fa-check-square-o" aria-hidden="true"></i>
      <i class="fa fa-times" aria-hidden="true"></i>
    </label>
  </li>
</ul>

<div id="output2"></div>

Answer №2

To handle multiple boxes, the functions showremove and showadd need to be modified. Instead of targeting a single id, they should target a class by changing selectbook and removebook. This way, an index can be added to access each individual box.

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

The functions getFiles and getFolders will consistently retrieve a single file or folder each time they are

When attempting to fetch all files and folders from my Google Drive, the function .getFiles() is only returning one file, while .getFolders() is only returning a single folder. However, I can confirm that there are multiple folders and files in my drive. ...

Implementing the row delete function in MUI DataGrid using JavaScript

My grid has a delete icon button in each row, but it's not deleting the row. Can someone please help me with this issue? I'm working on a todo app where each row should have its own delete button. I'm struggling to connect the deleteTodo co ...

Let's update the VUE app's development server to point to my-testing-web-address.com instead of the default localhost:

I am working on a VUE application and I am trying to run it on an external link instead of localhost. I attempted to configure a vue.config.js devServer: { host: 'http://my-testing-web-address.com', port: 8080, ... } and adjusted t ...

Having trouble implementing server-side rendering with Styled-Components in Next JS

I attempted to resolve my issue by reviewing the code and debugging, but unfortunately, I couldn't identify the root cause. Therefore, I have posted a question and included _document.js, _app.js, and babel contents for reference. Additionally, I disa ...

How can you merge webSDK with jQuery within a Vue Component?

I am currently working on incorporating the webSDK found at into our Vue application. My goal is to load jquery only within a single component. Below is the code I have written, however, when I click the button, it does not seem to function as expected. ...

Retrieve the information from a website and display it on the current webpage using an ajax request

Is there a way to insert parsed HTML content into my webpage using just a link from another page? I'm attempting to use an AJAX call, but I keep receiving an error. Below is the code I've written, and the browser doesn't seem to be the issue ...

Is there a way to prevent continuous repetition of JavaScript animated text?

I'm working on a code that displays letters and words one by one, but I can't figure out how to stop it from repeating. Can someone help me with this? <div id="changeText"></div> <script type="text/javascript"> var te ...

Enhancing the Appearance of HTML Select Dropdowns in JavaFX 2.2 WebEngine

I am currently working on a project that is unable to be updated to Java 1.8 to support the latest JavaFX version. While this may or may not impact the issue I am facing, I have been exploring various solutions from the internet to customize the look and f ...

How to display JSON containing nested objects in AngularJS using the ng-repeat directive

Hey everyone, I have this JSON external file that I need help with: { "success":true, "errors":[ ], "objects":[ { "cod":"8211300", "descricao":"Serviços advocatícios" }, // more objects here... ] } In ...

Launching ExpressJS and ReactJS on Heroku

Currently working on a project that combines express and react. When attempting to deploy it to Heroku via git push, I encountered an error upon checking the heroku logs. The specified webpage then shows a message indicating that it cannot locate a build ...

Mongoose Error: The function 'mongooseSchemahere' is not recognized as a valid function

Here is the mongoose Schema setup in models/user.js: const mongoose = require('mongoose'); const userSchema = mongoose.Schema({ loginId: String, firstname: String, lastname: String, eMail: String, password: String, acti ...

Utilizing fluent-ffmpeg in nodejs and express to effortlessly download a video

I've been recently tackling a side project that involves downloading videos from Reddit. The tricky part is that the video and audio are stored in separate files, requiring me to merge them before being able to download them onto the client's dev ...

The issue with AngularJS ng-show and $timeout functionality not functioning as expected

As a newcomer to AngularJS, I recently started an individual project utilizing ng-show and if else statements with $timeout. Despite my efforts, I have been unable to make the answers timeout after being displayed for a few seconds. I've tried various ...

How can we efficiently generate ReactJS Router for Links and seamlessly display a unique page for each Link?

Currently, I have an array of objects named titleUrl, which contains titles and URLs retrieved from an API. To display these as links on the sidebar, I am utilizing a custom component called MenuLink. The links are generated by iterating over the keys in t ...

Retrieving date from timestamp in a node.js environment

Can someone help me figure out how to display my timestamp as the date in the front end? I've tried multiple methods without success. Here is the code snippet: formulaire.addEventListener('submit', posteValidation); /** * Function to add a ...

Make sure to implement validations prior to sending back the observable in Angular

Each time the button is clicked and if the modelform is invalid, a notification message should be returned instead of proceeding to create a user (createUser). The process should only proceed with this.accountService.create if there are no form validation ...

What could be causing the server to return an empty response to an ajax HTTP POST request?

Attempting to make a POST request using ajax in the following manner: $.ajax({ type: "POST", url: 'http://192.168.1.140/', data: "{}", dataType: "json", ...

While trying to set up a development server in Firebase, I mistakenly deleted my build folder

I recently encountered an issue with my Firebase project. While trying to set up a development server for my existing React web app that is already in production, I ran into some unexpected problems. firebase use bizzy-book-dev firebase init firebase ...

Show information on a pop-up window using Ajax without having to open a new browser tab

I am currently working on a project that uses the Struts framework. In this project, I need to revamp the UI design. The existing project has a menu tab, and when each menu is clicked, a JSP page opens in a new browser window. However, I want these pages t ...

Adjusting the Connection header in a jQuery ajax request

I've been attempting to modify the Connection header using the code below, but so far, I haven't had any success jQuery.ajax({ url: URL, async: boolVariable, beforeSend: function(xhr) { xhr.setRequestHeader("Connection ...