I am looking to remove identical innerText values from two arrays using JavaScript

Is it possible to use JavaScript to remove the "added" className of an element with the same innerText when the delete button (.btn-wrap>li>button) is clicked?

<div class="table">
  <ul>
    <li class="added">list_1</li>
    <li class="added">list_2</li>
    <li class="added">list_3</li>
  </ul>
</div>
<ul class="btn-wrap">
  <li>
    <span>list_1</span>
    <button class="delete"></button>
  </li>
  <li>
    <span>list_2</span>
    <button class="delete"></button>
  </li>
  <li>
    <span>list_3</span>
    <button class="delete"></button>
  </li>
</ul>

Answer №1

An update has been made to my previous response: Upon reviewing the question and examining the provided HTML code again, I have revised my solution.

To clarify the question at hand: The original poster (OP) wants to remove a class from the list elements based on clicking the 'delete' button in a separate list below. I have adjusted my code snippet accordingly to achieve this functionality. Please let me know if this modification does not meet your requirements, so that I can further tailor my solutions to address any discrepancies.

const addedList = document.querySelectorAll('.added');

document.querySelectorAll('.delete').forEach(btn => {
  btn.addEventListener('click', e => {
    e.preventDefault();
    const deletedVal = e.target.previousSibling.previousSibling.textContent;
    addedList.forEach(listItem => {
      if (listItem.textContent === deletedVal) {
        listItem.classList.remove('added');
      }
    });
  });
});
.added {
color: blue;
}
<div class="table">
          <ul>
            <li class="added">list_1</li>
            <li class="added">list_2</li>
            <li class="added">list_3</li>
          </ul>
        <ul class="btn-wrap">
          <li>
            <span>list_1</span>
            <button class="delete">Delete</button>
          </li>
          <li>
            <span>list_2</span>
            <button class="delete">Delete</button>
          </li>
          <li>
            <span>list_3</span>
            <button class="delete">Delete</button>
          </li>
        </ul>
      </div>

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

Insert a new key-value pair into a PHP associative array retrieved from a database

Here's how the DB is currently set up: include 'php/DbConnect.php'; $sql = $mysqli->query( "SELECT t.*, v.* FROM task t INNER JOIN vocabtask vt ON (t.id = vt.taskid) INNER JOIN vocab v ON (v.id = vt.vocabid) WHERE vt.taskid = 1" ...

How to effectively pass custom props or data to the Link component in Next JS

As I dive into Next JS, I've hit my first roadblock. Currently, I am working on a page that showcases various podcast episodes with preview cards on the homepage. The card component code looks like this: import React from 'react'; import Li ...

Displaying client-side filtered rows in a jqGrid with postData filter upon initial loading

Our website includes a jqGrid that initially retrieves its rows using the built-in ajax fetch feature, returning a json object. We then apply filtering and searching on the client side by creating custom functions to generate postData filters. However, we ...

Error in NodeJS: 'Cannot alter headers once they have been sent.'

My project involves developing an app with Express that fetches tweets from Twitter. Each API call retrieves 200 tweets (or fewer if there are less than 200) and in total, I need to retrieve 1800 tweets. To achieve this, I use a time interval to make multi ...

Issue encountered when sending information to asmx web service via ajax and displaying the result on an HTML page with a javascript function

I have developed an ASMX web service that looks like this: [ScriptService] public class CurrencyData : System.Web.Services.WebService { [WebMethod] public string DisplayCurrency(double amount, string sign ,string style) { swi ...

Having trouble populating Extjs Grid with JSON data

In this simple grid, I am attempting to display three columns - Subject ID, Subject Name, and Subject Short Name by obtaining JSON data. The data is stored in a variable called myData in JSON format, which has been received from the server. Despite pasting ...

``"Selecting a location upon clicking a marker in the array

I have limited experience with javascript but I am determined to improve my skills. Currently, I am facing a major roadblock in a project that I am working on and urgently require assistance. The project involves creating a map with marked locations from ...

Having trouble correctly parsing XML data using JavaScript

My input field contains the following XML code: <?xml version="1.0" encoding="utf-8"?> <players timestamp="1536505850"> <player id="0518" name="Eagles, Philadelphia" position="Def" team="PHI" /> <player id="10271" name="Jones, Jul ...

Using jQuery to insert PHP code into a <div> element

In our capstone project, I'm looking to implement a dropdown calendar feature. I initially attempted to use ajax within a dropdown Bootstrap 4, but encountered issues with collapsing. As an alternative, I am considering utilizing the include method. A ...

Set markers at specific locations like "breadcrumbs" and generate a route using Google Maps API v3.exp

I'm developing a new iOS application for scouting out locations while on the move. I want users to be able to mark each location by simply clicking a button, which will drop a marker based on their current location. The ultimate goal is to connect all ...

Rotational orientation of a progress circle image in SVG

I am currently working on developing a circular progress bar, similar to the one shown in the image below. The progress of this bar is determined by percentages and it moves around the circle accordingly. I have managed to get the progression moving, b ...

Navigate the page by scrolling the absolute positioned div

Is it possible to make the fancybox modal scroll with the page using fancybox 2? I want it to move along with the content rather than being fixed in the center with a max-height restriction. How can I achieve this? $('.fancybox-open').fancybox({ ...

Creating a PNG image on a canvas, converting it to a data URL, and then transmitting it using an XMLHttpRequest in NodeJS/Express

I've been experimenting with different methods found on Google, but so far none have worked for me. I'm currently working on implementing the signature-pad, a JavaScript/HTML5 canvas solution for eSignatures. My goal is to save the canvas data as ...

issues with the redirection functionality in the Play framework

I am currently working on an application that extracts data from Twitter profiles. I want to give the user the ability to select which parts of their profile should be used (for example, excluding tweets). To accomplish this, I plan to incorporate checkb ...

Encountered difficulties sending JSON data to a REST endpoint using Node.js

Is there a way to bulk insert JSON data into MongoDB using Mongoose? I am aware of the insertMany method, but I'm encountering difficulties with extracting the correct req.body. Below is an image of my setup in Postman. https://i.sstatic.net/xFznd.pn ...

The issue of the background image not updating with jQuery persists in Internet Explorer 11

Hello everyone, I am facing an issue where I am trying to change the background image of a div using jQuery on click. The code I have written works perfectly on all browsers except for IE. Can someone please provide me with some guidance or help on how to ...

Is it possible to integrate a jQuery function within PHP code when encountering a "is not defined"

I am looking to implement the jQuery-confirm dialog in place of using a JavaScript alert generated by PHP. However, when I execute the following code, I encounter the following error message: $ is not defined echo "<script>$.alert({title: ' ...

Caught in the midst of a JSON update conundrum

I need some help with my JavaScript/JSON coding. I have a script that loads JSON data and displays it on an HTML page. Now, I want to know how I can update this data. Specifically, I want the script to update the location of the person when a button is cli ...

Webpage Text Animation

This is the visual representation of my webpage: https://i.stack.imgur.com/tYq34.png I am seeking to implement a uniform animation effect for the text "Carlos Qiano" and "Lorem Ipsum", similar to the link below: https://i.stack.imgur.com/XVnBS.jpg Note: ...

Navigate the JSON object at predetermined intervals, such as in the case of movie subtitles

Apologies if the title is not specific enough, open to any suggestions for improvement. Here's my issue: I have a JSON file (presented here as a JavaScript object) that contains subtitles for a movie. My goal is to display the text exactly as it appea ...