Incorrect element being returned by click event

Seeking assistance with creating a card that rotates upon clicking. The current setup is functional, however, I am aiming to utilize the onclick event to append the 'is-flipped' class to the classlist of div.card. Upon inspecting the event through console log, the target appears as

<div class="card__face card__face--front">front</div>
while currentTarget remains null. Initially, I suspected the issue might be related to absolute positioning and attempted implementing z-index: 100 without success. Why is this occurring and what approach should be taken to select the .card div?

// var card = document.querySelector(".card");
// card.addEventListener("click", function () {
//     card.classList.toggle("is-flipped");
// });

function handleCardClick(e) {
    // e.stopPropagation();
    console.log(e);
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
body {
    font-family: "Poppins", sans-serif;
}

body {
    font-family: sans-serif;
}

.wrapper {
    width: 200px;
    height: 260px;
    border: 1px solid #ccc;
    margin: 40px 0;
    perspective: 600px;
}

.card {
    width: 100%;
    height: 100%;
    transition: transform 1s;
    transform-style: preserve-3d;
    cursor: pointer;
    position: relative;
    z-index: 100;
}

.card.is-flipped {
    transform: rotateY(180deg);
}

.card__face {
    position: absolute;
    width: 100%;
    height: 100%;
    line-height: 260px;
    color: white;
    text-align: center;
    font-weight: bold;
    font-size: 40px;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

.card__face--front {
    background: red;
}

.card__face--back {
    background: blue;
    transform: rotateY(180deg);
}
<div class="wrapper">
    <div class="card" onclick="handleCardClick(event)">
        <div class="card__face card__face--front">front</div>
        <div class="card__face card__face--back">back</div>
    </div>
</div>

<div class="main" onclick="handleCardClick(event)">123456789</div>

Answer №1

To manipulate classes on the 'card' element, you can use the following code:

const card = document.querySelector('.card');
card.addEventListener('click', function(){
 card.classList.toggle('is-flipped')
})

If you prefer to use onclick="", make sure to add the "pointer-events: none;" property to the child elements of .card

.card__face {
pointer-events: none;}

Additionally, here is a function that handles clicking on a card element:

function handleCardClick(e) {

const currentEl = e.target;
currentEl.classList.toggle('is-flipped')}

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

Loop through each object in the array and create a new property by applying a regular expression to an existing property

Just starting out with Javascript and could use some guidance. Here is an array of objects that I have: var data = [{ name: 'name1', street: 'street2', person: 'person1', phone1: 'phone82 ', phone2: 'phone ...

IE11 encounters an error labeled SCRIPT1010, signaling an expected Identifier when compiled

Lately, I've been encountering a strange issue in Vue.js. Here's the thing: my application runs smoothly on all browsers locally (yes, even IE 11). But when I compile it using npm run build and deploy it to my server (which essentially serves con ...

Having trouble viewing PDF files after uploading them to S3. Uploading images is working without any issues in NodeJS

For a while now, I've been using the aws-sdk to upload images with no issues. However, I recently tried to upload a PDF and encountered a problem. Even though the upload seems successful, I get an error message saying Failed to load PDF document. I a ...

Utilizing THREE.JS Raycaster with JavaScript "entities" rather than just meshes

I am facing a challenge with the Raycaster model. I grasp the concept of how it intersects meshes that can be transformed, but my issue lies in identifying when the specific instance of an object is clicked. Consider a scenario where there is a button. Th ...

What is the best way to open a shared session in nightwatch and either a new browser tab or window?

When I attempted to use .sendKeys('body', [client.keys.COMMAND+"t"]), NW successfully sent the keys but unfortunately a new tab did not open. ...

When a div is clicked, the text inside the button changes. If another div is clicked, the previous text is reset

I am seeking a solution for changing the text of a button within three columns when a specific 'advice-card' div is clicked on. The text should change to 'Hide' for the clicked div, and the other buttons should switch back to 'Show ...

What causes the [$parse:syntax] error when I tap on the arrow icon within Google Maps?

.controller('MapCtrl', [ '$scope', '$http', '$location', '$window', function ($scope, $http, $location, $window) { $http.get('****').success(function (data, dealers, response) { ...

Utilizing square brackets in Node.js for working with URLs

While working in express.js, I encountered an issue when trying to add a router for the URL /xxx/xxx/items[5] that contains square brackets. The router functions correctly without square brackets but fails to work when they are included in the URL. Has a ...

Using C# Razor Pages to send checkbox values via AJAX requests

In my model class, I have the following: public class DataModel { public bool Display { get; set; } } After deserializing a FormData object to a JSON object, I am posting the value from a checkbox like this: this.FormToJSON = form => { const ...

Exploring Node troubleshooting with WebPack and Feathers

Currently, I am part of a team working on a project that involves using Node, Webpack, TypeScript, and Express/Feathers. While my fellow developers are experienced in these technologies, I have limited experience with JavaScript mainly on the client-side a ...

"Internet Explorer: Unleashing the Magic of Card Fl

I'm encountering a problem that I can't seem to solve. Everything works perfectly in Chrome, FF, Safari, etc., but in Internet Explorer, I see the image on the front side instead of the text on the backside. Can anyone please assist me with this ...

Using inline C# in the browser as an alternative to Javascript: a step-by-step guide

Recently, I came across Microsoft's Blazor, a tool that enables running C# code in the browser. After checking out some tutorials, it appears that an ASP.NET Core backend server is required for it to function properly. It would be interesting if we co ...

Navigating with Buttons using React Router

Header: I need help figuring out how to properly redirect to a new page when clicking on a Material UI button using the onClick method. Specifically, I am unsure of what to include in my handleClickSignIn function. Here is a snippet of code from my Header ...

Tips for properly updating state arrays in React.js

My quest to update an array in React.js using 'setState' led me to try different methods. Unfortunately, all my attempts seemed to replace the existing element rather than adding a new one. const [unplannedCards, setUnplannedCards] = useState([ca ...

SQL query returns a surprising outcome

Below is the table content (mTable) id | sent | number --------------------------------------- 23 | 2017-03-02 00:00:00 | 0 23 | 2017-03-04 00:00:00 | 0 45 | 2017-03-15 00:00:00 | 1.8 45 | 2017-03-17 00:00:00 | 1.9 id: i ...

What steps should I take to resolve the issue of missing files on my local website?

While working on my pokedex project using HTML, CSS, and JavaScript, I encountered an issue. When I try to open the project in VSCODE, everything works perfectly fine. However, when attempting to open the HTML file locally in a browser, it fails to find tw ...

Does JavaScript sort dictionaries automatically?

Here's an issue we're facing: when the ajax script receives a dictionary from the server, the order changes: The server sent this: {753: 'Wraith', 752: 'Phantom Extended', 751: 'Phantom', 750: 'Ghost Extended&a ...

How can I use jQuery to target and modify multiple elements simultaneously

I've been struggling for the past couple of hours trying to use prop to change the values of two items in a button. One item updates successfully, but the other one doesn't and I can't figure out why. Here is the HTML: <input type=&apos ...

Enhance the efficiency of highcharts when handling extensive datasets

I am looking to extract a higher volume of data. Here is a snippet of the sample data: 1850/01 -0.845 -0.922 -0.748 -1.038 -0.652 -1.379 -0.311 -1.053 -0.636 -1.418 -0.272 1850/02 -0.043 -0.113 0.047 -0.244 0.159 -0.613 ...

Please elaborate on the appropriate application of angularjs Directives in this specific scenario

From my experience with Angular, I've learned that directives are used for manipulating the DOM while controllers are more about controlling functionality. I've been struggling to convert a small wizard into generic directives, aiming for reusab ...