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

Retrieve the data object in VueJS

(Regarding a currency conversion tool) In order to accurately convert currencies, I am looking to access an object structured like this: rates: AUD: 1.708562 SGD: 1.546211 When retrieving these rates from an API, they are not always in the desired o ...

Does the built-in waiting mechanism in Protractor automatically handle promises?

While browsing through Stack Overflow, I stumbled upon this response in a discussion about Protractor tests and asynchronous solutions: 'AFAIK expect waits internally for the related promises.' I have tried looking through the official Protract ...

Transferring Data through the POST Method

Is there a way for me to send just one variable using the post method? I have retrieved the $row[id] from the database and need to include it in the form submission. We know how to send user input using dfs, but what about sending the specific $row[id] v ...

Clear the cache following the service call

I am currently working on a service that has two methods. Utilizing the built-in cache support for $resource, I am trying to implement a way to refresh the cache when a service call is made from the controller. I attempted to use $cacheResource without suc ...

Tips for dynamically updating an HTML value with Javascript

Summary of My Issue: This involves PHP, JS, Smarty, and HTML <form name="todaydeal" method="post"> <span id="fix_addonval"></span> <input type="radio" name="offer" id="offer_{$smarty.section.mem.index+1}" value="{$display_offe ...

What is the best way to bring Axios into the main.js file in a Vue project?

Recently, I encountered an issue with using axios in my Vue project. An error message stating that 'axios' is not defined popped up when I attempted to use axios.get() in my Home.vue file. I'm wondering if the problem lies within my configur ...

Script execution in '<URL>' has been prevented due to sandboxing of the document's frame and the absence of the 'allow-scripts' permission setting

When I deploy my pure Angular application with a REST API to a production server and try to access its URL from another site (such as a link in an email), I encounter a strange problem. Firefox doesn't provide any error message, but Chrome says: Blo ...

Can anyone provide a solution for determining the number of active intervals in Javascript?

Similar Question: How to View All Timeouts and Intervals in JavaScript? I've been working on an HTML5 game that includes a lot of graphical effects using intervals created by the setInterval function. However, I've noticed that my game is ru ...

Is there a way for me to scroll and bring the block up to the header when the button is clicked?

My goal is to create a functionality where clicking on the button with the class .booking__button will smoothly scroll the block up under the header. The position of the block should remain consistent, only the scrolling effect should be visible to the use ...

Working with radio buttons in a loop using PHP

I am facing an issue with a loop that iterates 3 times. In the loop, there's an HTML form containing radio buttons and I am processing the input using PHP. However, when echoing the form data, it does not display the correct values. Is there something ...

access denied on image links

While developing a web app with Angular5, I encountered an issue regarding retrieving image URLs from the 4chan API. Each post in my database contains an image URL, however, when I try to access it through my app, I receive a 403 forbidden error in the con ...

Storing data from an API response into the localStorage using Vue.js upon clicking

My objective is to save specific data in the browser's localStorage upon clicking a link. However, the output I receive is either undefined or completely empty. <li v-for="(category, index) in categories" :key="index"> ...

Incorporate multiple array values within an if statement

Looking to dynamically change the background image of an element based on specific strings in the URL. Rather than changing it for each individual term like "Site1", I want to be able to target multiple words. Here's what I've tried: var urlstri ...

Ways to convert a jQuery object into HTML that can be utilized?

When running the code below, an alert message of "object Object" is displayed: var shipImgs = $("#div").children(); alert(shipImgs); The container with id "div" includes a total of 4 children (image tags). <div id="div"> <img src="/imgs/spa ...

HTML5 for Bulk Video Upload

Is it possible to use the html5 api to record and upload video content directly from the browser? I am facing an issue in a project where the video recordings can be very long or large, causing extended upload times for users. Ideally, I would like the vi ...

Interactive calendar using Php and Javascript/Ajax

Currently, I am working on a PHP calendar and have integrated Javascript/Ajax functionality to enable smooth scrolling between months without the need for page refresh. Interestingly, the calendar displayed as expected before implementing Javascript/Ajax, ...

Guide to sending an HTML document containing images using the express framework

Is there a way to send an HTML file along with images using express? I have the following code that handles sending the initial page (index.html) to users. However, this page contains elements such as images and css files which are not being transferred p ...

What is the best way to transform an array of objects into MenuItems for a Material-UI Select component?

I am facing an issue with mapping the values of field choices to create options for an MUI Select field from an array called fieldChoices. Here is how I populate the fieldChoices array: fieldChoices = { choices: filtered_status.map(function (item) { ...

Encountering an abundance of concurrent requests using NodeJS and request-promise

I am currently working on a NodeJS project that involves a large array of about 9000 elements containing URLs. These URLs need to be requested using the "request-promise" package. However, making 9000 concurrent GET requests to the same website from the sa ...

Sketch a straight path starting from the coordinates x,y at a specified angle and length

Is there a way to draw a line in Javascript starting from a specific x/y position with a given length and angle, without having to define two separate points? I have the x/y origin, angle, and length available. The line should be placed on top of a regula ...