Clicking on a div will add a new class

How can I dynamically add a class to an element when clicking on a specific tag inside a div? Specifically, I want to add the class "show" when clicking on an anchor tag.

This is the HTML code I am working with:

<div class="liveChatContainer online">
<div class="actions">
<span class="item title">Need help?</span>
<a href="/test"><i class="fa fa-smile-o"></i>Chat</a>
<a href="/test"><i class="fa fa-smile-o"></i>Call</a>
<a href="/test"><i class="fa fa-smile-o"></i>Email</a>
</div>
<a href="#" class="liveChatLabel">Contact</a>
</div>

To see the issue in action and explore possible solutions, check out the JSFiddle link: http://jsfiddle.net/8wLze4rf/2/

Answer №1

Your fiddle has been updated, I believe this accomplishes what you were aiming for. When clicked, the class is added and another click (anywhere on the page) removes it.

$(".liveChatLabel").click(function(){
    $(".liveChatContainer").addClass("show");
});

$('html').click(function() {
    $(".liveChatContainer.show").removeClass("show");
});

$('.liveChatContainer').click(function(event){
    event.stopPropagation();
});

JSFiddle

Answer №2

  1. Include jQuery library.
  2. Implement the following code:

    $(document).ready(function() {
        $('.liveChatContainer a').click(function() {
            $('.actions a').removeClass('show');
            $(this).addClass('show');
            return false;
        });
    });

Answer №3

HTML

<div id="liveChatContainer" class="liveChatContainer online">
    <div class="actions">
        <span class="item title">In need of assistance?</span>
        <a href="/test"><i class="fa fa-smile-o"></i>Chat</a>
        <a href="/test"><i class="fa fa-smile-o"></i>Call</a>
        <a href="/test"><i class="fa fa-smile-o"></i>Email</a>
    </div>
<a id="liveChatLabel" href="#" class="liveChatLabel">Contact Us</a>
</div>

JS

$(document).ready(function(){ 
    $('#liveChatLabel').click(
        function(){ 
            $('.actions a').removeClass('show'); 
            $('#liveChatContainer').toggleClass('show'); 
            return false;       
        }); 
});

$(document).mouseup(function (e)
{
    var container = $("#liveChatContainer");

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.removeClass('show');
    }
});

JSFiddle Link

Answer №4

Utilizing toggle functionality to dynamically add and remove classes, you can view a demonstration here: http://example.com/toggle-demo

$(document).ready(function(){ 
var button = $('.interactionButton');

button.on("click",function(){

button.toggleClass('active');
});
});

Answer №5

$(document).click(function(e) {
  $('.liveChatContainer').toggleClass('show');
});

$('.liveChatContainer a').on('click', function(e) {
  $('.liveChatContainer').addClass('show');
  e.stopPropagation();
});
$('.liveChatContainer').on('click', function(e) {
  e.stopPropagation();
});
.show {
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="liveChatContainer online">
  <div class="actions">
    <span class="item title">Need assistance?</span>

    <a href="#"><i class="fa fa-smile-o"></i>Chat now</a>

    <a href="#"><i class="fa fa-smile-o"></i>Call us</a>

    <a href="#"><i class="fa fa-smile-o"></i>Email support</a>

  </div>
  <a href="#" class="liveChatLabel">Contact Us</a>

</div>

Answer №6

When working with JavaScript, remember to utilize setAttribute("class","actions") on your link element.

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

Combining three.js geometry and selecting objects with an octree

I have been utilizing Three.js to showcase a variety of custom geometries in different positions and rotations. These geometries are static and do not change frequently, but users have the ability to manipulate them by adding, deleting, or altering the sha ...

To navigate to the next page, simply click on the box instead of relying on text links

Here is a piece of code where you can click on the (account) text to open the next page. I am looking to modify this so that clicking on (.box.boxAccounts) will also take you to the next page, not just the text (account). The link_to function provides the ...

The edit functionality in jqGrid does not function properly if custom search parameters are designated

Using the Guriddo jqGrid JS version 5.2.0 implemented here: @license Guriddo jqGrid JS - v5.2.0 - 2016-11-27 Copyright(c) 2008, Tony Tomov, [email protected] The code block below showcases an entire self-contained implementation of jqGrid. It inclu ...

Finding the Absolute XPath of a Website Element

Using Javascript in the code below, I am retrieving the absolute XPath of a web element: public String getAbsoluteXPath(WebDriver driver) { return (String) driver.executeScript( "function absoluteXPath(element) {"+ "va ...

Issue with rendering dynamic data in a bar chart using React.js

I am currently facing an issue where I am trying to retrieve data from a database and display it as the x-axis labels in a bar chart. import React, { useState, useEffect } from "react"; import "../styling/ClassGrades.css"; import { Bar ...

How can I adjust the indentation in Angular Prime-ng's p-tree component?

In my project, I am utilizing the primg-ng tree component for the sidebar. Currently, the output is displayed as shown here: https://i.stack.imgur.com/kcSQt.png However, I am looking to maintain consistent indentation levels without any adaptive changes ...

Error occurred during the file watch process in PhpStorm after saving the CSSO file

Following the advice in the official PhpStorm documentation, I have implemented a CSS minifier. However, upon attempting to use it, I encountered the following error: cmd.exe /D /C call C:\Users\douglas\AppData\Roaming\npm\css ...

Only in local development, there was a discrepancy in the class name

I recently started working on an application using a combination of NextJS, React, and Styled Components. While everything seems to be functioning correctly in the production environment, I encountered a peculiar issue when running the application locally ...

I am puzzled by the fact that the center cell of my table is longer than the surrounding cells

I'm currently working on a project that involves creating a webpage. I have a table, and when I execute the code, I notice that the center cell appears longer than the rest of the cells. table,td,tr{border: 1px solid red; } <!document html> ...

What is preventing my HTML from rendering on the page?

I have cross-referenced the script below with the google charts documentation available at the following link: https://developers.google.com/chart/interactive/docs/gallery/scatterchart However, it is not displaying properly. I have reviewed the code and ...

What could be causing the Button disabled feature to malfunction after the page is reloaded?

I am looking for a way to temporarily disable the submit button after it is clicked, and then re-enable it after 10 minutes. I am using local storage to store this information. The issue I am facing is that when I reload the page, the Submit button is not ...

How can I create a JavaScript loop that continuously prompts the user for input until they type "exit"?

Can you help with a JavaScript challenge? I am looking to create a loop that continuously asks the user for input until they type "exit". Once they do, I want the program to display the minimum value that was entered. var students = prompt("Enter Stude ...

I encountered an issue: "SyntaxError: missing ) after argument list". What steps should I take to fix it?

I'm encountering the error message "SyntaxError: missing ) after argument list" and I need assistance in resolving it. Click here to view the code that is causing the error ...

Express.js does not recognize the req.query value

Incorporating Stripe Checkout functionality into a MERN application has been the focus of my recent project. Upon successful payment by a customer, they are directed to a checkout success URL where the CheckoutSuccess page is displayed. Additionally, Stri ...

Is using a div inside a li considered incorrect?

For instance; <ul> <li> <div class="img1"><img src="img.jpg" /></div> <div class="cs1">Some text</div> <div class="cs2">other text</div> <div class="button">Click here</div> ...

An issue has occurred with error code TS2688: The type definition file for 'jquery' cannot be located. [Angular Application] --

I am currently working on developing an Angular web application with a specific theme that requires the inclusion of CSS and JS files. Majority of the JS files needed are jquery plugins, so I made sure to install them using the following commands: npm i j ...

MQTT Broker specialized in Typescript

I'm looking to create a MQTT Broker using TypeScript and Angular. I've attempted a few examples, but I keep encountering the following error: Uncaught TypeError: http.createServer is not a function Here's a simple example of what I'm c ...

Utilize a script to set the src attribute of an invisible image to be the background image of a specific <div> element

I am seeking a way to empower my clients who use a CMS for their website to customize their background image. Unfortunately, the CMS does not offer direct access to edit the background-image feature. Therefore, I am looking to implement a solution using PH ...

Ways to prevent clicking on ng-repeat rows?

Is it possible to prevent clicking on ng-repeat rows? How can I achieve this? Below is a sample code snippet: <div ng-controller="MyCtrl" class="grid"> <div class="header"> <div class="cell">Id</div> <div class="cell" ...

The `value` attribute in the Dropdown component should be an array if the `multiple` option is enabled. The current type received is a `[object

Hey, I'm encountering an issue. The Dropdown component is throwing an error telling me that the value must be an array when multiple selection is enabled. The type received is: [object String]. Check out my code snippet here: https://codesandbox.io/ ...