Customize the DOM with tailwind CSS in a Vanilla JavaScript project

I am attempting to hide the "mark as complete" button and replace it with a "completed" button by switching the classes from "hidden" to "completed" and vice versa. However, when I use an event listener to manipulate the DOM with the code provided below, the changes are not reflected in the button itself.

const incompleteButton = document.querySelector(".Mark_complete");
const completedButton = document.querySelector(".completed");

incompleteButton.addEventListener("click", statusChanger, bgchange);
completedButton.addEventListener("click", statusChangerAgain);

function statusChanger() {
    if (completedButton.classList.contains("hidden")) {
        completedButton.classList.remove("hidden");
        incompleteButton.classList.add("hidden");
    }
}

function statusChangerAgain() {
    if (incompleteButton.classList.contains("hidden")) {
        incompleteButton.classList.remove("hidden");
        completedButton.classList.add("hidden");
    }
}
<div class="manager bg-white h-[70%] w-[70%] grid grid-cols-1 grid-rows-4 gap-2 shadow-2xl
shadow-gray-500">
  <div class="grid_item_1 bg-slate-300 flex justify-start items-center">
      <div class="status_heading">
          <p class="status text-3xl ml-8 mr-80">
              Name
          </p>
      </div>
      <div class="name_heading">
          <p class="name text-3xl mr-64">
              Status
          </p>
      </div>
      <div class="delete_heading">
          <p class="delete text-3xl">
              Delete
          </p>
      </div>
  </div>
  <div class="grid_item bg-red-400 flex justify-start items-center text-lg">
      <div class="task_name ml-8 mr-64">
          <p class="task_detail">
              <button>Call Mom!</button>
          </p>
      </div>
      <button class="Mark_complete mr-52 bg-gray-200 p-6 rounded-lg hover:scale-105">Mark as Complete</button>
      <button class="completed mr-52 bg-green-400 p-6 rounded-lg hover:scale-105 hidden">Completed!</button>
      <button class="Delete bg-red-500 p-6 rounded-lg hover:scale-105">Delete</button>
  </div>

The script links and CSS links have been properly added, ruling out any issues from that side

Answer №1

Kindly remove the bgchange from this specific line:

incompleteButton.addEventListener("click", statusChanger, bgchange)
By making this adjustment, you will resolve the issue at hand. Additionally, there is no need to duplicate code when you can achieve the same outcome with a singular function like so:

const incompleteButton = document.querySelector(".Mark_complete");
const completedButton = document.querySelector(".completed");

incompleteButton.addEventListener("click", statusChanger);
completedButton.addEventListener("click", statusChanger);

function statusChanger(e) {
e.target.classList.add("hidden");
  if(e.target == incompleteButton){
      completedButton.classList.remove("hidden");
  } else if(e.target == completedButton){
     incompleteButton.classList.remove("hidden");
  }
}
.hidden {
  display:none;
}
<div class="grid_item bg-red-400 flex justify-start items-center text-lg">
    <div class="task_name ml-8 mr-64">
        <p class="task_detail">
            <button>Call Mom!</button>
        </p>
    </div>
    <button class="Mark_complete mr-52 bg-gray-200 p-6 rounded-lg hover:scale-105">Mark as Complete</button>
    <button class="completed mr-52 bg-green-400 p-6 rounded-lg hover:scale-105 hidden">Completed!</button>
    <button class="Delete bg-red-500 p-6 rounded-lg hover:scale-105">Delete</button>
</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

Discovering the content within table cells by utilizing JavaScript functions linked to specific IDs

I am currently working on a project that involves a dropdown list: <select id="itemsList" onchange="gettingList()"> <option>Please choose an option</option> <option value="50">Shampoo</option ...

Guide on transferring Javascript array to PHP script via AJAX?

I need to send a JavaScript array to a PHP file using an AJAX call. Here is the JavaScript array I am working with: var myArray = new Array("Saab","Volvo","BMW"); This JavaScript code will pass the array to the PHP file through an AJAX request and displ ...

Ways to assign the value of an alert to an element

Within this piece of code, my intention is to retrieve the alert value and apply it to an element. Description: The AJAX code I have written checks for values in a database, fetches new values from the database, and then displays these fetched values in a ...

When using Jquery, hovering over an element will cause the full title to be

I have created a toggle div that displays an ellipsis (...) when the long title is longer than 30 characters. Now, I want the full text of the long title to appear when hovering over the div. Check out this JS Fiddle for reference. JS $('#popu ...

Is there a way to enable Tail Recursion Optimization in TypeScript?

const isPositive = (n: number) => n > 0; function fitsIn(dividend: number, divisor: number, count: number, accum: number): number { if (accum + divisor > dividend) { return count; } return ...

Encountering a problem when utilizing window.ethereum in Next Js paired with ether JS

Experiencing some difficulties while utilizing the window.ethereum in the latest version of NextJs. Everything was functioning smoothly with NextJs 12, but after upgrading to NextJs 13, this error started popping up. Are there any alternative solutions ava ...

Encountering ERR_EMPTY_RESPONSE when using the $.POST method

I have been struggling with a issue on my social networking site that includes chat functionality. I have utilized the $.post method extensively across multiple pages, and it generally functions well except for a specific page called message.php where user ...

What is the best way to start the server when the files are located in separate directories?

I have organized my project into two separate folders, one for the client and one for the server Is there a way to use npm start to simultaneously run both the frontend and backend scripts? Check out the screenshot below: View of Two Folders in my Projec ...

Manipulate the lines in an HTML map and showcase the distinctions between them

I've been searching through various inquiries on this particular subject, but none have provided me with a satisfactory response. I have created a map where I've set up 4 axes using the following code: function axis() { var bounds = ...

I am having trouble calling a method in the subscribe function because it is showing an error message saying "Cannot read property 'showMessageFromSocket' of undefined"

My goal is to call a method within the subscribe block, but I am encountering an error that says "Cannot read property 'showMessageFromSocket' of undefined". How can I successfully call the showMessageFromSocket method? In Angular, this was possi ...

Using PHP and JQuery to disable a button after the letter "U" is typed

I am looking for a way to disable the button when the term "U" (defined as Unable) appears. How can I achieve this? Below is the button in question: <input type="submit" class="form-control btn-warning" name="search" value="Search Data"></input& ...

Top approach for triggering and re-triggering AJAX using php

I'm in the process of deciphering instructions from my developer. Our approach may not be entirely correct, so I want to ensure that we are on the right track. This is our objective: On a webpage, there exists a button. When this button is clicked, I ...

Running the command "npm start" in ghost (node.js) is causing an error

After successfully installing node.js, I attempted to set up Ghost. However, when running the installation for Ghost using npm install --productions I encountered the following error: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" da ...

Issue with current time malfunctioning

For my latest project, I've been tasked with creating a karaoke application using javascript. The challenge is to synchronize the lyrics with the song as it plays. Here's a snippet of my HTML code: <div id="lyric"></div> <audio i ...

The error "Call to a member function exports() on array" occurs when attempting to use the

As I attempt to send an array of values to the jobExport() collection, I encounter an error stating Call to a member function jobsExport() on array. I comprehend the necessity for the collection to be populated with modal collection value. However, my goal ...

Tips for preserving the status of a sidebar

As I work on developing my first web application, I am faced with a navigation challenge involving two menu options: Navbar Sidebar When using the navbar to navigate within my application, I tend to hide the sidebar. However, every ti ...

Tips for avoiding a form reload on onSubmit during unit testing with jasmine

I'm currently working on a unit test to ensure that a user can't submit a form until all fields have been filled out. The test itself is functioning correctly and passes, but the problem arises when the default behavior of form submission causes ...

The array is arranged properly, yet React is failing to render it in the correct order

Here's the code snippet I am working with: { this.state.rows.map((qc) => qc.BinsByDayByOrchardsQCs.map((qc2) => qc2.BinsByDayByOrchardsQCsDefects.map((qc3) => !defectsArray.includes(qc3.Defect) &am ...

Error: Unable to locate module: 'material-ui/styles/colors'

I encountered an issue with the code below, as it failed to compile: import React from 'react'; import { AppBar, Toolbar } from 'material-ui'; import { Typography } from 'material-ui'; import { MuiThemeProvider, createMuiThem ...

Implementing a delete functionality within a loop on a JavaScript object array

I have a JavaScript object with the following structure: var partner = { p_name: { value: partner_name, label: "Name" }, p_id: { value: partner_ID, label: "ID" }, p_status: { value: partner_status, label: "Status" }, p_email: { value: partner_emai ...