Is Toggleclass malfunctioning?

I have been working on making the 'timelineTile' div open and close, as well as making it close if another one is opened or if the background is clicked. I have successfully implemented the functionality to close the div when another one is clicked or when the background is clicked, but I am struggling to get the div to close when clicked on itself. You can see my code in action on this JSFiddle.

Here is a snippet of my code:

$(function () {
    $('.timelineTile').click(function (evt) {
        evt.stopPropagation();
        $('.selected').children().removeClass('clicked');
        $(this).toggleClass('clicked');
    });

    $(document).click(function () {
        $('.timelineTile').removeClass('clicked');
    });
});
.timelineTile.clicked{
    background:red;
    width:500px;
    height:300px;
    -webkit-transition:height 1s, left 1s, -webkit-transform 1s;
    transition:height 1s, left 1s, transform 1s;
}

.selected {
    -webkit-transition:height 1s, left 1s, -webkit-transform 1s;
    transition:height 1s, left 1s, transform 1s; 
}


.timelineTile {
   background:black; 
    color:white;
    width:200px;
    height:100px;
    margin-bottom:20px;
    -webkit-transition:height 1s, left 1s, -webkit-transform 1s;
    transition:height 1s, left 1s, transform 1s;
}

.timelineTilehold {
    background:pink;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul class="timelineTilehold">
    
    <li class="selected"><div class="timelineTile">hello
    </div></li>

<li class="selected"><div class="timelineTile">hello
    </div></li>

<li class="selected"><div class="timelineTile">hello
    </div></li></ul>

Answer №1

Prevent the removal of class on the clicked element by using .not()

$('.timelineTile').click(function (evt) {
    evt.stopPropagation();
    $('.selected').children().not(this).removeClass('clicked');

Check out the DEMO here

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

Display a <div> element styled using CSS3

Question: I am facing a challenge with CSS3 styling. I have one class and one div. The class is currently set to display:none, but I want it to show when the mouse hovers over it (display:block). I have attempted the following: .1:hover + div#2 { dis ...

The current state of my DOM doesn't match the updated value in my model. The update is triggered by a function that is called when a button is clicked

app.controller('thumbnailController', function ($scope) { debugger $scope.customers = [ { name: 'Ave Jones', city: 'Phoenix' }, { name: 'Amie Riley', city: 'Phoenix&ap ...

most effective method for recycling dynamic content within Jquery mobile

My jQuery mobile app has a requirement to reuse the same content while keeping track of user selections each time it is displayed. I have successfully created the necessary html content and can append it to the page seamlessly. The process goes something ...

Can you explain the extent to which JavaScript's setTimeout() and clearTimeout() apply?

Approximately 30 seconds after a page is loaded or reloaded, a pop-up will be displayed under certain conditions. The following code successfully achieves this functionality: jQuery(document).ready(function($) { .......... if (localStorage.getItem ...

What is the most efficient way to combine a parameter string within a JavaScript function and append it to an HTML string?

Welcome to my HTML code snippet! <div id="content"></div> Afterwards, I add an input to #content: $( document ).ready(function() { // Handler for .ready() called. var parameter = "<p>Hello</p>"; $("#content").appe ...

Identify a specific HTML template using jQuery

I am currently working on implementing datepickers in various HTML templates. The issue I am facing is that the functionality only seems to work in my index.html file. When I try to replicate the same setup in another template, it does not function proper ...

Alert: An error has occurred with the DataTables table identified as 'datatable' while utilizing Laravel for Ajax functions

While using jQuery AJAX Datatable, an error keeps popping up on my browser. I have double-checked my code and can't find any mistakes. Can anyone help me figure out what's wrong? I want the data stored in $details to be displayed in a datatable. ...

Having difficulty in setting the react-player light prop to always true

I have incorporated a slider feature with videos and their names. Each slide contains a video with its name, displayed using the [react-player][1] component. When a video is clicked, a modal opens to play the video. The issue I am facing is that despite se ...

Getting the value of a variable inside an onclick function

I am facing an issue with displaying the values of 2 variables inside an onclick event. I have tried the code below but it is not working. Can someone please help me solve this problem within the next 3 hours? var myCode = "12345"; var myCount = "5" $(&a ...

IE browser transparency effect on canvas

I have encountered an issue when drawing a shape on canvas using kineticJS and setting the fill to none. The code snippet I am using is below: var rect = new Kinetic.Path({ x: 0, y: 0, data: 'm 2.0012417,2.0057235 ...

Implementing Laravel Blade Popup for Supporting Multiple Languages

I'm facing an issue while trying to implement multi-language support in a Laravel project. The problem arises when I try to add a confirmation alert. For instance, onclick="confirm( {{ __('Are you sure you want to remove this method? The ...

Guide on retrieving information from the World Bank API using jQuery/AJAX despite receiving a success status code of 200, but encountering an error

I am trying to retrieve data in JSON format using the code provided at this link: . Despite specifying dataType: jsonp, the request is returning a status 200 and the data is received but the success handler is not being triggered, instead the error block ...

Changing the background color with a switch function in ReactJS

After clicking a link, a view is rendered based on the "id" from a JSON. To enhance the user experience, I want to add a background color when a particular view renders and also toggle the style. This code snippet illustrates how the crawl is displaye ...

What is the best way to configure a CakePHP method to generate standard HTML output when the controller is set to default JSON?

Currently, I am working on developing an Android application and utilizing CakePHP on the server side. I have a UsersController that provides JSON data for functionalities like login, registration, etc, except for account activation. As account activatio ...

Achieve a flat text indentation similar to an ordered list using CSS

Is there a way to format my FAQ Q & A like an ordered list? <div class="col-left"> <h3><strong>CAPTION</strong></h3> <ul> <li>Q: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed non risus. Suspe ...

Radio button triggers an ajax call once, but then fails to function

How can I troubleshoot an issue where the ajax function only works once when clicking on a radio button to change its value from 0 to 1, but not back to 0? The form contains a table with radio buttons for each record, and clicking on them triggers the aj ...

Utilizing Javascript to Connect with Java Web API

I'm seeking assistance with transferring a file from a browser to another device connected to a server-operated machine. I am relatively new to web application and back-end programming. The current code allows for moving a file from the server to the ...

Angular Material Spinner with Custom Image Icons - (mat-spinner)

I have successfully implemented the mat-spinner in my project with configurable changes like color and mode of spinning. However, I am now looking to add an image icon, specifically the logo of a brand or company, inside the spinner. How can I achieve this ...

Create an XPath by utilizing the class and text attributes of a div element

Struggling to create a relative xpath for use with webdriver, none of the ones I've attempted seem to be working. Here is my Web Element: <div class="pa-PrimaryNavWidget-hyperlink" style="color: rgb(255, 255, 255); cursor: default; background-col ...

Determine whether the browser tab is currently active or if the user has switched to a different

Is there a way to detect when a user switches to another browser tab? This is what I currently have implemented: $(window).on("blur focus", function (e) { var prevType = $(this).data("prevType"); if (prevType != e.type) { // handle double fir ...