Switching between three div elements along with two icons can be done by using toggle functionality

Is there a way to make the icon change back when clicked again without making major changes to the existing code? Looking for suggestions on how to achieve this functionality.

function myFunction() {
  var element = document.getElementById("demo");
  element.classList.toggle("change");
}

function myFunction1() {
  var element = document.getElementById("demo1");
  element.classList.toggle("change");
}

function myFunction2() {
  var element = document.getElementById("demo2");
  element.classList.toggle("change");
}

$('.column1').click(function() {
  var collapsed = $(this).find('i').hasClass('fas fa-check');
  $('.example-class').find('i').removeClass('fas fa-check-circle');
  $('.example-class').find('i').addClass('fas fa-check');
  if (collapsed)
    $(this).find('i').toggleClass('fas fa-check fas fa-check-circle')
});
.change {
  border-color: blue;
  color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="row1">
  <div id="demo" onclick="myFunction()" class="column1">
    <i class="fas fa-check"></i>
    <h2>Learn</h2>
    <p>I'm here to</p>
    <p>look around</p>
  </div>
  <div id="demo1" onclick="myFunction1()" class="column1">
    <i class="fas fa-check"></i>
    <h2>Start</h2>
    <p>I want to find my</p>
    <p>first help desk</p>
  </div>
  <div id="demo2" onclick="myFunction2()" class="column1 clk">
    <i class="fas fa-check"></i>
    <h2>Switch</h2>
    <p>I'm interested in</p>
    <p>swtiching help desks</p>
  </div>
</div>

Answer №1

Streamlined Approach Using jQuery

$('.column1').click(function(){
  $(this).addClass('active').siblings().removeClass('active');
});

No need for additional functions or IDs, just utilize a common class

$('.column1').click(function(){
  $(this).addClass('active').siblings().removeClass('active');
});
/* Helpful CSS */

.column1.active{
  color:blue;
  border: 1px solid blue;
 
}
.fs5::before{
  content:'\f00c';
  font-family: "Font Awesome 5 Free"; 
  font-weight: 900; 
  
}
.active .fs5::before{
  content:'\f058';
  font-family: "Font Awesome 5 Free"; 
  font-weight: 900;   
}
/* Additional Random CSS */

.column1 {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
    padding: 0 5px 
}
.column1 * {
    margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="row1">
  <div class="column1">
    <span class="fs5"></span>
    <h2>Learn</h2>
    <p>I'm here to</p>
    <p>look around</p>
  </div>
  <div class="column1 active">
    <span class="fs5"></span>
    <h2>Start</h2>
    <p>I want to find my</p>
    <p>first help desk</p>
  </div>
  <div class="column1">
    <span class="fs5"></span>
    <h2>Switch</h2>
    <p>I'm interested in</p>
    <p>swtiching help desks</p>
  </div>
</div>

Answer №2

function updateIcon(element) {
  // Toggle the class on main element
  element.classList.toggle("change");
  // Toggle both check and circle check classes
  element.querySelector('i').classList.toggle('fa-check')
  element.querySelector('i').classList.toggle('fa-check-circle')
}
.change {
  border-color: blue;
  color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<div class="row1">
  <div id="demo" onclick="updateIcon(this)" class="column1">
    <i class="fas fa-check"></i>
    <h2>Learn</h2>
    <p>I'm here to</p>
    <p>look around</p>
  </div>
  <div id="demo1" onclick="updateIcon(this)" class="column1">
    <i class="fas fa-check"></i>
    <h2>Start</h2>
    <p>I want to find my</p>
    <p>first help desk</p>
  </div>
  <div id="demo2" onclick="updateIcon(this)" class="column1 clk">
    <i class="fas fa-check"></i>
    <h2>Switch</h2>
    <p>I'm interested in</p>
    <p>swtiching help desks</p>
  </div>
</div>

Answer №3

Do you find this modification helpful? I updated your click(function(){});.

function myFunction() {
  var element = document.getElementById("demo");
  element.classList.toggle("change");
  
}

function myFunction1() {
  var element = document.getElementById("demo1");
  element.classList.toggle("change");
}

function myFunction2() {
  var element = document.getElementById("demo2");
  element.classList.toggle("change");
}

$('.column1').click(function() {
  var collapsed = $(this).find('i').hasClass('fas fa-check');
  if (collapsed)
    $(this).find('i').toggleClass('fas fa-check fas fa-check-circle')
    
    if(!collapsed)
    $(this).find('i').toggleClass('fas fa-check fas fa-check-circle')

});
.change {
  border-color: blue;
  color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="row1">
  <div id="demo" onclick="myFunction()" class="column1">
    <i class="fas fa-check"></i>
    <h2>Learn</h2>
    <p>I'm here to</p>
    <p>look around</p>
  </div>
  <div id="demo1" onclick="myFunction1()" class="column1">
    <i class="fas fa-check"></i>
    <h2>Start</h2>
    <p>I want to find my</p>
    <p>first help desk</p>
  </div>
  <div id="demo2" onclick="myFunction2()" class="column1 clk">
    <i class="fas fa-check"></i>
    <h2>Switch</h2>
    <p>I'm interested in</p>
    <p>swtiching help desks</p>
  </div>
</div>

Answer №4

Here's a great suggestion for you. Have you heard of alpin.js? It can simplify things for you.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"
      integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ=="
      crossorigin="anonymous" referrerpolicy="no-referrer"/>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23424f534a4d466355110d5b0d5b">[email protected]</a>/dist/alpine.min.js"
        defer></script>

<div class="row1" x-data="sampleData">
    <template x-for="item in items">
        <div id="demo" @click="check($event, item)" class="column1"
             :class="{change:item.checked}">
            <i class="fas"
               :class="item.checked?'fa-check-circle':'fa-check'"></i>
            <h2 x-text="item.title"></h2>
            <p x-text="item.line1"></p>
            <p x-text="item.line2"></p>
        </div>
    </template>
</div>

<style>
    .change {
        border-color: blue;
        color: blue;
    }
</style>

<script>
window.sampleData = {
    items: [
        {
            title: 'Lean',
            line1: 'I am here to',
            line2:'look arround',
            checked: false
        },
        {
            title: 'Start',
            line1: 'I want to find my',
            line2: 'first help desk',
            checked: false
        },
        {
            title: 'Switch',
            line1: 'I\'m interested in',
            line2: 'swtiching help desks',
            checked: false
        },
    ],
    check(e, item) {
        item.checked = !item.checked
    }
}

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

Is it feasible to style individual letters in a word within an input field?

Is it possible to change the styling of individual letters in an input containing text? For example, if the word 'Test' is in the input, can I make the 'Te' bold while leaving the 'st' regular? Alternatively, perhaps I'd ...

How can you convert Promise.all to a single Promise?

I have successfully implemented two API calls using Promise.all method with no issues. Even though one API call is sufficient to retrieve the results, I decided to stick with Promise.all and it's still working fine. I attempted to replace Promise.al ...

Arranging and structuring Handlebars templates

In this particular example... http://example.com/1 ...I am experimenting with organizing a Handlebars template and its corresponding content in separate files, as opposed to having them all combined in the HTML file or JavaScript script, like in this con ...

Creating layers of object declarations

Looking for assistance on the code snippet below. type Item = { id: number; size: number; } type Example = { name: string; items: [ Item ]; } var obj: Example = { name: "test", items: [ { i ...

Issue with Chrome styling of li and a elements persists after link click

In my current project, I am facing an issue with my menu displaying differently in Firefox and Chrome. When clicking on the links in Chrome, they tend to move around, disrupting the styling of the menu. A common suggestion I found was to use display: block ...

iOS is causing issues with the JavaScript function and not allowing it to

By creating an NSString that contains JavaScript, I am able to set the e-mail body. NSString * someString =@"<!DOCTYPE html>\n <html>\n <body> \n <h1>My Web Page</h1> \n <p ...

Angular - the utilization of expressions in view templates

Exploring Angular for the first time and attempting to create a Single Page Application (SPA). I have included the route module, which seems to be functioning properly. However, the templates are not interpreting Angular expressions as expected - for examp ...

In the event that the "li" element contains an "

<ul> <li> <ul></ul> </li> <li></li> <li></li> <li></li> <li> <ul></ul> </li> </ul> Is there a way to specifically a ...

Expanding Bootstrap 5 navbar-nav to occupy entire screen space in collapsed state

Hello everyone, I hope you are doing well! I am currently working on a website project using Bootstrap 5 and have encountered an issue with the navbar toggler. While the toggler is functioning properly, I am having trouble making it fill the full width o ...

How to deactivate an option in md-autocomplete

I encountered a scenario where I converted an md-input-container with a md-select/md-option to a md-autocomplete. While in the initial setup of md-select/md-option, we were able to apply ng-disabled property to both the md-select and md-option. However, wh ...

Animation Effects in Opera and Internet Explorer

Just unveiled my latest project, CSSload.net - an awesome tool for generating CSS spinners and bars. The animations are running smoothly on Firefox and Webkit browsers. Curious if anyone has tips on animating elements like this in Opera and IE? ...

Having trouble resolving an error while attempting to incorporate an npm module into a vanilla JavaScript application

I admit my lack of expertise in building modern JavaScript apps is becoming evident. Currently, we have a Capacitor app that uses plain JavaScript without any build tools, and it functions well. Our goal is to incorporate Microsoft Code Push support throu ...

In Mobile View, I want to swap out my current image for text to keep it hidden

I am attempting to include text that is only visible in Mobile View as a replacement for my logo. Below is the code I am using: CSS: @media only screen and (max-width:1024px) { header .logo { display: none; } header .logo a { display: non ...

Establish limits for draggable item

I am working on a project where I have a draggable element generated dynamically using jQuery. The div containing the element also has a background image. How can I ensure that the draggable element never goes outside of the boundaries of the div? If you ...

"Utilizing images within an iframe: A step-by-step guide

I'm trying to integrate an iframe into a phone image with a transparent background. However, I am unsure how to effectively mask the iframe so that it only appears within the boundaries of the phone image. .phone { display: block; position: r ...

Working with Node.js and JavaScript's Date object to retrieve the time prior to a certain number of hours

I am currently working on a script in node.js that is able to locate all files within a specific directory and retrieves their modified time: fs.stat(path, function(err, states){ console.log(states.mtime) }) After running the script, it ...

Extract information from a Web Service using Javascript

As a novice in web app development, I am currently working on creating a login page and retrieving user data from a local database using JavaScript. Despite my efforts, I seem to have made an error somewhere in my code. Below is the snippet of my JavaScrip ...

How can I create a comment section using jQuery, Ajax, and PHP?

There are 50 pictures where you can either add or delete comments. What is the most effective way to implement this feature? 1) Should I create a separate form for each image, input the data, and then post it? (Would require creating 50 forms for adding c ...

Invoke a function within one component using another component

I am facing an issue with deleting playlists displayed on my page. These playlists are fetched from an external API and each playlist has a delete button which is supposed to remove the playlist both from the API and the view. The deletion process works s ...

Troubleshooting problem with Bootstrap navbar collapsing when certain items are not collapsed

I am currently working on creating a Bootstrap menu for a website that has certain items that should not collapse on extra-small screens. While I have made progress in implementing this feature, I have encountered an issue. When the screen size is extra-s ...