Difficulty encountered in closing div by clicking the background with the help of jquery

I am facing a challenge with properly closing a div container and restoring it to its original state when I click outside of it. Despite trying various solutions from stackoverflow and extensive internet research, I have been unable to find or come up with a working solution. I attempted using stopPropagation but the issue persists where clicking on the background after the initial interaction continues to toggle the div. You can view a simplified version of my code in this js fiddle https://jsfiddle.net/DTcHh/10545/

Below is the jQuery snippet:


    <script> 
var text = 1;

$('.login1').click(function(e){
     $('.div2, .div3, .patientAccess').toggle("fast");
     $('.loginform_hidden').toggleClass('loginform_visible ');

    if(text == 1){

        $(".div1").toggleClass("animateSlide col-xs-12");
        $('.login1').html('Go Back');
        $('.imageOne').toggleClass('animateSlideTop');

        text = 0;
    } else{
        $(".div1").toggleClass("animateSlide");
        $('.login1').html('Start Animation');
         $('.imageOne').toggleClass('animateSlideTop');

        text = 1;
    }
});

$('.login2').click(function(){
    $('.registrationform_hidden').toggleClass('registrationform_visible ');
        $('.div1, .div3, .patientAccess').toggle("fast");
    $('.imageOne').toggleClass('animateSlideTop');
    if(text == 1){
        $(".div2").toggleClass("animateSlide2");
        $('.login2').html('Go Back');

        text = 0;
    } else{
        $(".div2").toggleClass("animateSlide2");
        $('.login2').html('Start Animation');

        text = 1;
    }
});

$('.div3').click(function(){
    $('.imageOne').toggleClass('animateSlideTop');
        $('.div1, .div2, .patientAccess').toggle("fast");
    if(text == 1){
        $(".div3").toggleClass("animateSlide3");
        $('.div3').html('Go Back');

        text = 0;
    } else{
        $(".div3").toggleClass("animateSlide3");
        $('.div3').html('Start Animation');

        text = 1;
    }
});
      </script>

Answer №1

It seems like I grasp your inquiry correctly.

To address the issue, you can implement an event listener to detect clicks outside of the highlighted section and then remove the highlighting by calling .removeClass() when clicked elsewhere.

I believe I have comprehended and provided a solution to your query. Please let me know if this resolves the issue for you.

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

The Angular framework's structure is loaded in a combination of synchronous and asynchronous ways once the primeng tableModule

Encountered this error while trying to load the TableModule from primeng into my components module file and running 'npm run packagr': Maximum call stack size exceeded To address this, I switched my primeng version from primeng12 to primeng11.4. ...

There was an issue encountered while trying to install Electron using the command 'npm install electron'

While attempting to install electron using npm install electron, I encountered the following error: 908 error code 1 909 error path C:\Users\name\Documents\name\Code\code\node_modules\gl 910 error command failed ... ...

Is it necessary to have height:0 and width:0 when display:none is already applied?

Is it necessary to include height:0 and width:0 when applying display:none to a class? I've noticed these properties being used in certain instances, could they potentially resolve any issues with compatibility in older browsers? ...

ASP.NET MVC2 JsonResult: Sorry, this request has been denied

Although this question may sound familiar, I just can't seem to get over it. This section pertains to my Controller Action: public JsonResult AddToCart(int pId, int qty = 1, int optVal = 0) { AjaxActionResult response = new AjaxActionResult(); r ...

Troubleshooting issues with ember-data's belongsTo relationship

I am facing an issue with the model I have: Whenever I make a call to this.store.find('history'); A request is sent to http:://www.example.com/api/histories/ and I receive the following JSON response: { "tracks":[ { "id":83, ...

Utilizing bootstrap for a responsive design that includes max-height constraints and horizontal

Seeking to modify the appearance of my Angular Template by incorporating Bootstrap and a custom CSS file. <!-- index.html --> <div static-include="partial/example.html"></div> The static-include directive, sourced from Stack Overflow, p ...

jQuery blueimp File Uploader: Transferring multiple files to the server on Internet Explorer

After customizing the demo for my Rails application, I encountered an issue with the plugin in Internet Explorer. While it works smoothly on Chrome and Firefox, in IE (all versions) it fails to upload all but one file when multiple files are selected, and ...

Implementing user authentication in Rails with Devise and Backbone framework

I am just getting started with backbone.js. Currently, I am working on a rails application utilizing the "backbone-on-rails" gem. After successfully incorporating 3 models and rendering views with backbone, now I am looking to implement authentication usin ...

Tips on controlling the color of an input field in a form using React

Within my React app, there is a text input field situated inside a form that displays in its default gray color before any interaction: https://i.stack.imgur.com/FdNos.png Once the input field is clicked on, it changes to white as shown here: https://i.s ...

Tips for dynamically updating the value of a variable in Vue by importing a JavaScript file

There is a file for an app that imports ymaps.js where YmapsComponent.vue is declared. import '../js/ymaps.js'; import { createApp } from 'vue'; const app = createApp({}); import YmapsComponent from './components/YmapsComponent.vue ...

Timeout during the beforeLoad event

I am currently working on writing some ExtJS 4 script and have come across the following code: var companyStoreModel = Ext.create('Ext.data.Store', { model: 'CompanyDataModel', proxy: { type: 'ajax&apos ...

Error: Attempted to update user profile with an invalid function in Firebase

After creating an Avatar Chooser, I am encountering an error when selecting an image. Instead of displaying the selected image in the avatar, the error message is being displayed. How can I resolve this and ensure that the image appears in the Avatar Icon ...

Struggling to set the background color for a div element

I am dealing with the following HTML and CSS: <div id="com_loaded"> <div id="com_loaded_userpic"><a href="#" class="tooltip"><img src="pic.jpg" class="img_poster" /><span>Username</span></ ...

Position a button and input element in alignment

I recently decided to challenge myself by recreating the Netflix registration page as a practice project. Using the flexbox model on the webpage, I encountered an issue with aligning the email input and the "Get Started" button. Despite trying different me ...

What is the distinction between revealing environment variables in Next.js using the next.config.js as opposed to utilizing the NEXT_PUBLIC prefix?

As stated in the nextjs documentation, to make my environment variables accessible in the browser, I can simply prepend them with NEXT_PUBLIC in my .env.local file, like this: NEXT_PUBLIC_VAR=7 However, it seems that another approach is available where I ...

Leverage the sync function within the await .then("sync") method in JavaScript

If a synchronous function is called within the .then part of an asynchronous await .then, such as: await asyncFunc(...) .then(res => sendMSG(res)) .catch(err => next(err)); function sendMSG(res){ xyz } The sync function sendMSG i ...

"Uncovering the mystery of the missing element in Jquery's open

I have developed a click-to-show feature that opens and closes a div from left to right. You can view the demo here on jsfiddle.net In the demo, you will find a green-colored div. Upon clicking this div, another div opens from left to right. An interesti ...

Express Module Employs Promises for Returns

I have a JavaScript file for elasticsearch (could be any other database as well) that performs a simple query and uses a promise to return the data. I am using this module in my Express server (server.js) with the hope of retrieving the data, as I ultimat ...

Glowing sphere in Three.js

Can someone help me with a coding dilemma I'm facing? I'm trying to create a sphere that functions as a source of light, like the sun. I've been experimenting with the meshPhongMaterial options emissive: color and shininess: intensity, but s ...

Having difficulty styling scrollbars using CSS

There is currently a scrollbar displaying over part of my page when necessary. https://i.stack.imgur.com/48Rbx.png I want to change the styling of the scrollbar to make it less bright and have a transparent background. I attempted to apply some styles, bu ...