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

Would combining node.js with express for the back-end and emberjs for the client side be a suitable choice for my project?

As a seasoned web developer, I have limited experience with nodejs and have yet to dive into the world of emberjs (although I have worked extensively with backbone). I am embarking on a new project to create a web-based writing application focused on lite ...

"Why is the comparison function of bcryptjs returning null even though the hash being used is the one that was generated

For security purposes, I securely store hashed passwords in MongoDB. However, when I attempt to compare the stored password with a user-entered string, bcryptjs returns a null value. https://i.stack.imgur.com/4sTXM.png The hashed password is stored in bin ...

What are some ways to optimize Ajax requests for improved speed when multiple identical requests are made on a single webpage?

When the webpage is loaded, a block is dynamically created using an Ajax call to retrieve data from another page. This structure is then populated and added to a specific DOM element. However, multiple Ajax calls during page loads are causing delays. Is ...

The ngAfterViewInit lifecycle hook does not get triggered when placed within ng-content

The ngAfterViewInit lifecycle hook isn't triggered for a Component that is transcluded into another component using <ng-content>, as shown below: <app-container [showContent]="showContentContainer"> <app-input></app-input> ...

JavaScript error: forEach is not a function

I've encountered an issue while attempting to loop through a JSON object to extract data. Whenever I run my code, I receive this error: Type Error: element.listing.forEach is not a function. It's worth mentioning that I've used this method ...

Executing a Visual Basic subroutine from a jQuery dialog box

Currently, I have a form that includes a jQuery dialog creation. Within this dialog, there is a button generated from an ASP server control that should trigger a function in the code behind when clicked. However, I am encountering an issue where the functi ...

Can someone provide a list of events for the .on function in Vanilla NodeJS?

Currently experimenting with NodeJS by testing basic backend functionalities like sending various HTTP requests from my index.html file to the server.js file. I plan to delve into Express soon. I've noticed a lack of documentation on NodeJS 'eve ...

Creating a pluggable application in Node.js: A step-by-step guide

I am embarking on creating a Node.js and Express CMS with the following folder structure: MyCMS plugins themes uploads index.js I aim to load plugins from the plugins folder: plugins sample-plugin awesome-plugin ...

Listeners for JavaScript actions

My typical approach to implementing a select box is as follows: <select size="1" name="example_length" onchange="callSomeMethod"> <option value="10" selected="selected"></option> <option value="25">25</option> < ...

substitute elements within an array using elements from a different array

Looking to swap object(s) in an array with another array while maintaining the original order. Here is arrayOne: [ { "color": "#f8edd1", "selected": true }, { "color": "#d88a ...

How can I send and retrieve multiple variables in a URL using WordPress?

There seems to be an issue where I can only retrieve the first variable, specifically "product_category," from the URL http://localhost/coffeesite/?product_category=coffee&brand=bourbon. When I output JavaScript to confirm that the variables are set, ...

Clicking to close tabs

I am currently working on implementing a tab functionality on my website and I want these tabs to be responsive. Here is the code snippet I have been using: function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.ge ...

Implementing jQuery form validator post anti-SPAM verification?

I am facing what seems like a straightforward JavaScript issue, but my knowledge in this area is still limited. Following a successful implementation of basic anti-SPAM feature that asks the user to solve a simple math problem, how can I integrate jQuery& ...

What is the best method for transmitting server errors to the client?

When using passport.js, I have all the required code in place. However, I am facing an issue with displaying errors on the client-side. Despite having the successRedirect set up correctly, nothing happens when a wrong password is entered, and no error mess ...

Does CSS in the current IE9 beta allow for text shadows to be implemented?

Text shadows look great in Firefox and Chrome. For example, I have a basic website for streaming videos. Unfortunately, there are no shadows in IE9 for me. This is my CSS code: p { color: #000; text-shadow: 0px 1px 1px #fff; padding-bottom: 1em; } ...

What could be the issue with my JSON file?

I am currently utilizing the jQuery function $.getJson. It is successfully sending the desired data, and the PHP script generating the JSON is functioning properly. However, I am encountering an issue at this stage. Within my $.getJSON code, my intention ...

Implementing Vue plugins in your Store: A step-by-step guide

Looking for the proper way to integrate a plugin within a Vuex module or plain JS module. Currently using an event bus but unsure if it's the best approach. Any guidance would be appreciated. Plugin1.plugin.js: const Plugin1 = { install(Vue, optio ...

Retrieve the location of the selected element

We are faced with the challenge of determining the position of the button clicked in Angular. Do you think this is achievable? Our current setup involves an ng-grid, where each row contains a button in column 1. When the button is clicked, we aim to displ ...

Using base64 encoding and setting the binary type for Websockets

I'm feeling a bit lost. I've been working on understanding the mechanics of Websockets by setting up a server in node.js and a client-side application. One thing that's really puzzling me is how Websockets handle data transmission. Should da ...

Unable to replace default typography in MUI with custom typography theme on Next.js

In my Next.js React project, I am utilizing Material-UI (MUI) with a customized theme. Although the colors from the theme are being applied successfully, I am encountering difficulty in adjusting the default font sizes of H2 and H5 elements. Even though I ...