Having trouble with the click button flip function? It seems to be working in one section but not in

Currently, I am facing an issue with a card section that contains two buttons and a description. The first button adds an image which is working perfectly fine, as well as the description section. On the other hand, the second button adds a video and when clicked, it flips the card to reveal some information behind it. However, the problem arises when I replicate the same code for another section. At this point, the flip function of the first section works, but the flip function of the second section does not work; meaning only one flip function works at a time.

https://i.stack.imgur.com/wcsZa.jpg

https://i.stack.imgur.com/YvzhD.jpg

<script>
 // Init Simple Cropper
$('.cropme').simpleCropper();

$('#portrait').hide();
$('.switch').click(function (){
    $(this).text("Switch to "+($('#portrait').is(":visible") ? "Portrait" : "Landscape"));
  $('#portrait').toggle();
  $('#landscape').toggle();
});
</script>

<script>
  document.addEventListener('DOMContentLoaded', function(event) {

  document.getElementById('flip-card-btn-turn-to-back').style.visibility = 'visible';
  document.getElementById('flip-card-btn-turn-to-front').style.visibility = 'visible';

  document.getElementById('flip-card-btn-turn-to-back').onclick = function() {
  document.getElementById('flip-card').classList.toggle('do-flip');
  };

  document.getElementById('flip-card-btn-turn-to-front').onclick = function() {
  document.getElementById('flip-card').classList.toggle('do-flip');
  };

});
</script>
.flip-card-3D-wrapper {
  position: relative;
  -o-perspective: 900px;
  -webkit-perspective: 900px;
  -ms-perspective: 900px;
  perspective: 900px;
  margin: 0 auto;
}
#flip-card {
  width: 100%;
  height: 400px;
  text-align: center;
  position: absolute;
  -o-transition: all 1s ease-in-out;
  -webkit-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
  -o-transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  -ms-transform-style: preserve-3d;
  transform-style: preserve-3d;
}
.do-flip {
  -o-transform: rotateY(-180deg);
  -webkit-transform: rotateY(-180deg);
  -ms-transform: rotateY(-180deg);
  transform: rotateY(-180deg);
}
#flip-card-btn-turn-to-back {
  position: absolute;
  top: 46%;
  right: 29%;
  width: 145px;
  height: 46px;
  background: white;
  cursor: pointer;
  visibility: hidden;
  font-family: 'Open Sans', sans-serif;
  font-weight: 600;
  font-size: 13px;
  padding: 0;
  border-radius: 50px;
  color: #000;
}
#flip-card-btn-turn-to-front {
  position: absolute;
  top: 2%;
  left: 2%;
  width: 40px;
  height: 40px;
  background: white;
  cursor: pointer;
  visibility: hidden;
  font-family: 'Open Sans', sans-serif;
  font-weight: 600;
  font-size: .7em;
  padding: 0;
  border: 1px solid #00666a;
  border-radius: 50px;
}

#flip-card .flip-card-front, #flip-card .flip-card-back{
  width: 100%;
  height: 100%;
  position: absolute;
  -o-backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  -ms-backface-visibility: hidden;
  backface-visibility: hidden;
  z-index: 2;
}
#flip-card .flip-card-front {
    background: #1fa5ad;
}
#flip-card .flip-card-back {
    background: #00666a;
    -o-transform: rotateY(180deg);
    -webkit-transform: rotateY(180deg);
    -ms-transform: rotateY(180deg);
    transform: rotateY(180deg);
    width: 350px;
    height: 400px;
    right: 0%;
}
#flip-card .flip-card-front p, #flip-card .flip-card-back p {
  color: #fff;
  display: block;
  position: absolute;
  width: 80%;
  left:8%;
  font-size:14px;
  font-family: 'Open Sans', sans-serif;
  margin-top: -167px;
  padding: 20px!important;
  transform: translateX(0px);
}
#flip-card .flip-card-front p, #flip-card .flip-card-back p a{
  color: #ffc20e;
  text-decoration: none;
}
<div id="flip-card">
                 <div class="flip-card-front">
                  <div class="cropme" id="landscape1" style="width: 350px; height: 400px;"></div>
                   <button id="flip-card-btn-turn-to-back">Add Video</button>
                    <textarea name="form_message" class="form-control required border-r-none ht-gallery-new" rows="5" placeholder="Add Description" aria-required="true"></textarea>
                  </div>
                 <div class="flip-card-back"><div class="inner">
                   <div class="description">
                      <p><strong>Note:</strong> <a href="https://www.youtube.com/" target="_blank">Click</a> here to see how to find embedded video url</p>
                        <input type="text" required="" placeholder="Paste Embedded Video Url Here" name="gmap" class="form-control allow_only_alphabet space video-url-new">
                        <textarea name="form_message" class="form-control required border-r-none add-des-new" rows="5" placeholder="Add Description" aria-required="true"></textarea>
                       </div>
                    </div>
                <img src="images/return-btn.png" id="flip-card-btn-turn-to-front" class="return-btn-bg"></div>
              </div>

Answer №1

Your flip cards contain 2 unique elements that currently share the same ID. It would be more beneficial to refactor your code to utilize classes instead of IDs.

Instead of completely overhauling all your code, I have created a simple example for you to follow. Pay attention to how I avoid using ID tags and observe how I handle the javascript. Once you eliminate the ID tags, make sure to update your CSS (.flip-card instead of #flip-card) and your javascript. Refer to this example as a guide to restructuring your code:

document.addEventListener('DOMContentLoaded', function(event) {
  document.querySelectorAll('.flip-button').forEach(div => {
    div.addEventListener('click', e => {
      e.target.closest('.flip-card').classList.toggle('do-flip');
    });
  });
});
.flip-card {
  width: 100%;
  height: 400px;
  text-align: center;
  position: absolute;
  -o-transition: all 1s ease-in-out;
  -webkit-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
  -o-transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  -ms-transform-style: preserve-3d;
  transform-style: preserve-3d;
}

.do-flip {
  -o-transform: rotateY(-180deg);
  -webkit-transform: rotateY(-180deg);
  -ms-transform: rotateY(-180deg);
  transform: rotateY(-180deg);
}
<div class='flip-card'>
  Card 1<br>
  <button class='flip-button'>&lt;</button> <button class='flip-button'>&gt;</button>
</div>
<div style='height:100px;'></div>
<div class='flip-card'>
  Card 2<br>
  <button class='flip-button'>&lt;</button> <button class='flip-button'>&gt;</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

What is the best way to navigate through multi-dimensional arrays of objects in JavaScript?

I am trying to access JavaScript objects that are stored in a multi-dimensional array, which is being exported by a WordPress plug-in. Unfortunately, I cannot make changes to the code to use a single array. There are two arrays called "employees". Is this ...

Exploring ways to check async calls within a React functional component

I have a functional component that utilizes the SpecialistsListService to call an API via Axios. I am struggling to test the async function getSpecialistsList and useEffect functions within this component. When using a class component, I would simply cal ...

Sorting method in Ext JS 6.2.0 using mode

Seeking clarification on the sort([field],[direction],[mode]) method in Ext JS 6.2.0. Can someone explain the distinction between append, prepend, replace, and multi as mentioned in the documentation available at this link? I am unable to find a clear expl ...

Having trouble displaying the accurate model value in the alert

I have the following piece of code: $(document).ready(function () { var edit= @Model.Edit.ToString().ToLower(); if( edit !=true ) { $("#editWriteup").hide(); } var date= @Model.EndDate; alert(date); ...

Converting a json array into a map with the help of Underscore.js

My challenge involves parsing a JSON array into a map object, with the key being the state and the value being an array of objects that share the same state. An example JSON data set is provided below: { "totalRec": 10, "content": [ { "name" ...

Creating an AJAX data form in a JSP scenario

I want to correctly set up the data parameter for the ajax call. <script type="text/javascript"> $(document).ready(function() { $('#call').click(function () { $.ajax({ type: "post", ...

JS Issue with Generating Content

Introduction( kind of :) ) Starting with the process of generating 5 coffee beans for each cup of coffee. The coffee class includes a strength attribute, and my goal is to visually distinguish the coffee beans which have a strength greater than the select ...

Unspecified variable in AngularJS data binding with Onsen UI

I am new to Onsen UI and AngularJS, and I have a simple question about data binding. When I use the variable $scope.name with ng-model in an Onsen UI template, it returns as UNDEFINED. Here is my code: <!doctype html> <html lang="en" ng-app="simp ...

Issue: Unable to open port (GetCommState) : Error code 1 not recognized - Utilizing Nodejs, express, SerialPort

I am currently attempting to establish a connection between a fiscal printer with serial input and nodejs. I am utilizing the SerialPort module, but encountering difficulty in establishing the connection. The console is displaying the following error messa ...

What is the best way to target the final child element of a specific CSS class?

I am working with an HTML table and my goal is to remove the border-bottom from the last row, excluding the row with class .table-bottom: <table cellpadding="0" cellspacing="0" style="width:752px;" class="table"> <tr class="table-top">< ...

Attempting to display a singular form

Currently, I am working on a MERN app and encountering a small issue... I am developing an application where users can create rooms and within those rooms, they can plan their daily activities. It's similar to a TODO app but more intricate. I wanted ...

Execute a JavaScript code prior to sending a response directly from the ASP.NET code behind

On my .aspx page, I have the following code which prevents the page from responding without confirmation: <script type="text/javascript"> window.onbeforeunload = confirmExit; function confirmExit() { return 'Are you sure you wan ...

Formatting decimals with dots in Angular using the decimal pipe

When using the Angular(4) decimal pipe, I noticed that dots are shown with numbers that have more than 4 digits. However, when the number has exactly 4 digits, the dot is not displayed. For example: <td>USD {{amount| number: '1.2-2'}} < ...

Enhancing the functionality of angular-messages is impacting the overall design of

Displaying here is the current state of my login form: However, subsequent to upgrading angular-message to version 1.4 and higher, the layout transforms into this: Sharing my source code for reference: <ion-view view-title="Login"> <ion-hea ...

Discover the secret to instantly displaying comments after submission without refreshing the page in VueJS

Is there a way to display the comment instantly after clicking on the submit button, without having to refresh the page? Currently, the comment is saved to the database but only appears after refreshing. I'm looking for a solution or syntax that can h ...

Issue: "TypeError: Unable to retrieve dynamically imported module" encountered while utilizing dynamic imports in Remix application

I am currently facing an issue with dynamic imports in my Remix application. Whenever I try to dynamically import a module using the code below: const module = await import(../lib/lang/lang-${language.toLowerCase()}.js); An error occurs: TypeError: Fail ...

Personalize Autocomplete CSS based on the TextField input in React Material UI when a value is present

In my current project, I am utilizing React Material Autocomplete fields, which includes a nested TextField. I have successfully implemented standard styles for when the field is empty and only the label is visible, as well as different styles for hover ef ...

Show the checked options retrieved from controller

I am facing an issue with displaying certain checkbox values from the controller. In order to properly save these values, I believe it would be best to declare them in the controller and then use them in the HTML. However, my current code is not successful ...

How to show a button within a table cell using AngularJS?

The AngularJS application developed by my company was recently handed over to me for further development. Currently, I am working on integrating a navigation button in a table cell that will allow users to navigate to custom pages of their choice. Within ...

The ƒ character doesn't seem to be a match for the JavaScript regex

I am facing a requirement to only allow (extended) ASCII characters in my input. As a solution, I've implemented a JavaScript regex pattern like this: /^[\x20-\xff]+$/.test("helloê¿£×جáƒ") However, this doesn't work as expect ...