I attempted to craft a toggle button by applying and removing an active class that I had previously designed, but unfortunately, it did not function as intended

Every time I click on a button, I keep encountering this error message. I am certain that my selector is correct, but I can't seem to figure out why I'm getting the Uncaught TypeError: Cannot read property 'classList' of undefined at HTMLInputElement. (xx.js:55)

const activateButtons = () => {
    const buttons = document.querySelectorAll('[type="radio"]');
    buttons.forEach(button => {
        button.addEventListener('click', () => {
            button.parentdNode.classList.add('active');

            buttons.forEach(otherButton => {
                if (otherButton !== button) {
                    otherButton.parentNode.classList.remove('active');
                }
            });
        });
    });
};
activateButtons()
.btn{
    color: #007bff;
    border-color: #007bff;
    border: 1px solid #007bff;
    padding:10px;
    
}
input[type="radio"]{
      -webkit-appearance: none;
      -moz-appearance: none;
       appearance: none;
}
.active{
  background-color: #007bff;
  color: #FFF;
}
<!--//button-->
    <div class="btn-group btn-group-toggle" style="margin-bottom: 1rem">
        <label class="btn btn-outline-primary active" >
            <input type="radio" name="source"  autocomplete="off" checked> Button A
        </label>
        <label class="btn btn-outline-primary">
            <input type="radio" name="source"  autocomplete="off" >  Button B
        </label>
    </div>
    <!--//button-->

Answer №1

The correct spelling is 'parentNode', not 'parentdNode'

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 is the reason for typescript's lack of a "function" type?

As a newcomer to TypeScript, I'm puzzled as to why I am unable to define an object like this: const obj: { property1: string property2: boolean property3: function } It seems that the only workaround is to use: const obj: { property1: strin ...

The standard color code applied to an HTML button

I need to create an anchor element <a> that resembles an HTML button. However, I am struggling to find the default background color for the input type button. ...

A pair of div containers with one set to adjust its height automatically and the other to

I have a container with a height set to 100%. Inside, there are two child divs. Is it achievable to achieve the following heights: First div equal to the height of its content Second div equal to the remaining free space in the container ...

Streamlining Complex Javascript IF Statements

I came across a suggestion online for making an if statement more concise: if([1,5,7,22].indexOf(myvar)!=-1) alert('yeah') Instead of the longer version like this: if( myvar==1 || myvar==5 || myvar==7 || myvar==22 ) alert('yeah') Is ...

Error: The configuration object is invalid, and I am unable to deploy my server to test the bundled code

After running the webpack --mode production command to build the dist folder, I encountered an error when trying to run the server as the app is still running in developer mode. The error message displayed was: C:\Users\Bymet\Documents&bs ...

Having trouble setting up a page layout with CSS grid or grid-template-areas

Hey everyone, I'm a beginner in the world of HTML, CSS, and JS and this is my first time reaching out for help. I've been searching through various forums and spending quite some time on this particular issue, but unfortunately, I haven't fo ...

What are the best techniques for organizing SCSS in Next.js?

There are multiple pages with some unused items. Is there a method to search and delete all the items based on their classname? Appreciate any assistance you can provide! ...

Unable to conceal adjacent radio buttons

My challenge is to use only HTML and CSS, without JavaScript, to show the first radio button with its label initially. When a user clicks on it, the second radio button and label should appear while the first disappears, and so on. I am struggling to ach ...

Exploring the functionality of promises in JavaScript

Currently, I am using the most recent version of Angular. The code snippet I've written looks like this: $q.all({ a: $q.then(func1, failHandler), b: $q.then(func2, failHandler), c: $q.then(func3, failHandler), }).then(func4); Is it guaranteed ...

What could be the reason behind my useEffect() not triggering when updating a value in a React context?

How come my useEffect hook doesn't trigger when I update a dependency that involves a React context? In my project, the context is used within a wizard to gather user data and then process it on the final step. I am able to update the "Person" fields ...

Having trouble getting Firestore/Firebase to work with Next.js?

Hey everyone, I could really use some help here. I've been working on integrating Firebase into my Next.js app for the API. Everything works well when I build and run locally, but once I deploy to Vercel for production, I encounter a 500 - Internal S ...

Struggling to store information in a MongoDB database within my MEAN Stack project

After successfully creating a collection for user LOGIN/LOGOUT and managing to store and retrieve data using Express app and mongoose, I encountered an issue when trying to create another collection. Despite successfully creating the collection, the data w ...

Unable to retrieve real-time data from Firestore using getStaticPaths in Next.js

While fetching data from Firebase Firestore using getStaticProps is successful, I encounter a 404 page when attempting to implement the logic for retrieving details of individual items with getStaticPaths. The current state of my [id].js code appears as fo ...

Protractor tests succeeding prior to complete page load

Recently, my protractor tests have been failing after updating the node_modules. Strangely, it seems like the tests are initiating before the page is fully loaded: Connecting to the selenium server at http://127.0.0.1:4444/wd/hub [launcher] Running 1 inst ...

Issue with displaying data using a custom pure pipe and boolean in ngIf condition

For my current web project, I require a friendship/follow feature. There are two roles involved: admins and regular users. Regular users have the ability to follow other users, while admins do not possess this capability. When a user wishes to follow anot ...

CSS overflowing issue causing inability to reach bottom of div with scroll bars not functioning as anticipated

My objective is to create a layout featuring a sticky header and left sidebar, with a non-sticky DashboardBody (the green-bordered box) that can be scrolled through. When scrolling, I want the content at the top to disappear "under" the sticky header. The ...

Is there a way to incorporate my personalized styling and HTML code into Django forms?

I've been struggling to customize form styling in Django. I want to exclusively use my own styles for the forms. How can I achieve this? <form id="contact" action="" method="post"> <div class="row"> ...

How can I shift the button's location within a pop-up in Ionic 1?

enter image description here I am struggling to make the button green underneath the three other buttons. Can someone please assist me with this? Here is my code: $scope.showIntroductionPage = function(childs){ if(childs == 0){ var myPopup = $io ...

Submitting data to an external PHP file using the JavaScript function $.post

Having issues with the PHP page function ajaxFunction(){ var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequ ...

Angular.js is experiencing difficulties when using the input value attribute in conjunction with ng-model

I've been hard at work on an app that allows users to edit items, with those changes updating in a database. To prevent empty form submissions, I automatically fill the input fields with the current item's information. form.form-update(method="p ...