Utilize CSS animation classes on multiple elements throughout a single webpage

Trying to create a share button for social media with an animated button using CSS. However, encountering issues when trying to display multiple buttons on the same page for different content. The problem arises when clicking the second button, as it triggers animation in the first button instead. How can I ensure that the CSS is applied correctly to every button on my page?

Here's the fiddle: https://jsfiddle.net/2s6w4hq7/

This is my HTML:

<title>Profill</title>
        <br>
        <br>
        <br>
        <br>
        <div class="share">
            <div class="share-button">
                <input class="toggle-input" id="toggle-input" type="checkbox" />
                <label for="toggle-input" class="toggle"></label>
                <ul class="network-list">
                    <li class="twitter">
                        <a href="#">Share on Twitter</a>
                    </li>
                    <li class="facebook">
                        <a href="#">Share on Facebook</a>
                    </li>
                    <li class="googleplus">
                        <a href="#">Share on Google+</a>
                    </li>
                </ul>
            </div>
        </div>
        <br>
        <br>
        <div class="share">
            <div class="share-button">
                <input class="toggle-input" id="toggle-input" type="checkbox" />
                <label for="toggle-input" class="toggle"></label>
                <ul class="network-list">
                    <li class="twitter">
                        <a href="#">Share on Twitter</a>
                    </li>
                    <li class="facebook">
                        <a href="#">Share on Facebook</a>
                    </li>
                    <li class="googleplus">
                        <a href="#">Share on Google+</a>
                    </li>
                </ul>
            </div>
        </div>
    

This is the CSS:

/* 
    Using FontAwesome for icons 
    https://fortawesome.github.io/Font-Awesome/
    */
    .share-button {
        position: relative;
        width: 50px;
        margin: 5px;
    }

    ... (CSS continues)
    

Answer №1

Make sure to assign a unique id to each button and link the <label>'s for attribute to that specific id:

Here's an example for your reference:

https://jsfiddle.net/9d3g5kaf/2/

          <input class="toggle-input" id="toggle-input-1" type="checkbox" />
          <label for="toggle-input-1" class="toggle"></label>

          <input class="toggle-input" id="toggle-input-2" type="checkbox" />
          <label for="toggle-input-2" class="toggle"></label>

Answer №2

It's really straightforward. All you need to do is update the id attribute of the input element and use the same id in the label.

For example:

<input class="toggle-input" id="toggle-input1" type="checkbox" />
<label for="toggle-input1" class="toggle"></label>

Replace your current code with the code above.

Answer №3

To create unique IDs for Checkbox and label within the div with class share-button, follow the code example below:

      <title>Profill</title>
<br>
<br>
<br>
<br>
<div class="share">
        <div class="share-button">
          <input class="toggle-input" id="toggle-input" type="checkbox" />
          <label for="toggle-input" class="toggle"></label>
          <ul class="network-list">
            <li class="twitter">
              <a href="#">Share on Twitter</a>
            </li>
            <li class="facebook">
              <a href="#">Share on Facebook</a>
            </li>
            <li class="googleplus">
              <a href="#">Share on Google+</a>
            </li>
          </ul>
        </div>
    </div>
    <br>
    <br>
    <div class="share">
        <div class="share-button">
          <input class="toggle-input" id="toggle-input1" type="checkbox" />
          <label for="toggle-input1" class="toggle"></label>
          <ul class="network-list">
            <li class="twitter">
              <a href="#">Share on Twitter</a>
            </li>
            <li class="facebook">
              <a href="#">Share on Facebook</a>
            </li>
            <li class="googleplus">
              <a href="#">Share on Google+</a>
            </li>
          </ul>
        </div>
    </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

jQuery: Managing and modifying values for dynamically generated input elements

Hello, I am currently working with the latest version of jQuery and have a table set up where clicking on the "Add" button appends a row of inputs. My goal is to calculate the total price for each particular product based on keyup events (i.e., qty * price ...

Tracking the last time an app was run in Javascript can be achieved by creating a variable to store the date when

Exploring the world of Javascript as a newbie, I find myself with index.html, stylesheet.css and div.js in my app. Within div.js lies a code that magically generates a schedule for our team members over 2 weeks. However, there's a catch - consistency ...

Is it possible to have more than one button for each item on my Shopping List App?

I am developing a shopping list app that allows users to add items to a list. Each item added triggers the display of a button next to it, enabling users to remove the item from the list when needed. However, a problem arises as more items are added – i ...

Discover the nearest class and smoothly expand it with the slideDown function in jQuery

Hey there, I'm working on implementing a "View More" button on my page. The button will reveal another unordered list that currently has the class of "hidden-list". When the user clicks on "View More", I want it to slideToggle the hidden-list element ...

Options for regular expressions in CSS: match letters regardless of case and search for all occurrences

Here is the HTML I am working with: <div> <img class="hi" src="http://i.imgur.com/f9WGFLj.png"></img> <img class="HI" src="http://i.imgur.com/f9WGFLj.png"></img> </div> Additionally, I have the following CSS co ...

What is the process for retrieving wallet transactions using Alchemy websockets?

I am trying to retrieve all new transactions from a specific wallet using the code provided. However, I am encountering an issue when using the function tx["transaction"]["from"] to filter transactions based on the specified wallet. I am utilizing Alchemy ...

I'm having trouble with emailjs in HTML. Why am I not receiving any emails from emailjs? And how can I successfully send emails using emailjs

How can I successfully send emails using emailjs in an HTML project? Previously, I had no issues sending emails with emailjs in Reactjs, but now in my HTML CSS JavaScript project, it doesn't seem to be working. Could someone assist me with implementin ...

Can you walk me through how async/await works?

Can someone provide a detailed explanation of this code? const promiseFactory = () => new Promise(resolve => setTimeout(() => resolve(1), 5000)); If I execute the following: const consumer = async() => { promiseFactory().then(s => con ...

Incorporating unique typography onto your website (HTML/CSS)

I am currently struggling to implement a custom font on my website. I'm not very experienced with programming, and since my friend isn't available to help, I'd appreciate a solution from anyone. The issue I'm facing is that while the Ca ...

Using JavaScript to Set Values and Manage Session State in APEX

Trying to utilize JavaScript in Oracle APEX to set the value and session state. See below function that is being called: function updateItemValue(element) { $s('P2020_SELECTED', element); apex.server.process ('MY_PROCESS', { ...

Retrieve data from the table and dropdown menu by clicking a button

A script is in place that retrieves data from two columns (Members, Description) dynamically from the table upon button click. Table html Here is the JQuery code responsible for extracting values from the table: $(function() { $('#myButton') ...

Access-Control-Allow-Origin does not permit the origin null

I've been searching for an answer to this question, but I haven't found a suitable one yet. let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState === 4 && xhr.status === 200){ let response = r ...

Problem with incorporating responsive gifs

I'm new to CSS and I'm trying to incorporate an animated GIF as a smartphone screen using Bootstrap to ensure responsiveness. I've managed to get it working for larger and medium screens, but the issue arises when resizing for smaller displa ...

Utilizing Redux actions in React components

As I was delving into React and Redux concepts, I decided to create a simple component to gain a better understanding. Now, I am attempting to divide the Redux logic into a separate component, resulting in a total of two components. However, upon compili ...

Having trouble displaying Ad-Gallery Loader.gif?

Having trouble with an image gallery I found at . The loader.gif image isn't showing up in IE9, just a red X. Being new to JavaScript, I'm struggling to fix this issue located at the top of the JavaScript file. (function($) { $.fn.adGallery = ...

How do I stop Bootstrap from taking over my custom navbar design?

My website's navbar stopped working after adding Bootstrap to my HTML pages. I suspect that Bootstrap is overriding my custom CSS. Here is the code for my navbar: .navbar { overflow: hidden; !important; background-color: #333; !important; } .. ...

Deciphering a PHP json_encode response using JavaScript

Consider this scenario where I am using json_encoding for my array and displaying it: $key = $this->key; $sql = "SELECT * FROM Requests"; $result = $this->db->query($sql); ...

Obtaining information from an AngularJS Service Response and assigning it to the Controller Scope Object

I have a basic AngularJS 1.7 application where I am successfully fetching data from a web API using an AngularJS service. However, I am running into an issue where the data is not being populated in the controller scope object. I have verified that the dat ...

Why Does Angular's ngIf Always Result in a Negation to True?

Can someone please clarify this for me? I'm having trouble understanding why the *ngIf condition and else statement always evaluate to true unless I am completely mistaken. Here is the situation: I have an icon that should turn the background color t ...

Exploring the dissimilarity among npm run dev and the parcel index.html

I have been using parcel index.html to set up a local development server, bundling, and hot module replacement. However, I recently learned that npm run dev can do something similar. This has left me wondering: What are the distinctions between the two me ...