Close button for body

I have created a form that floats in the center of the screen

On this form, I have included a button designed to close it

However, I would like for the form to be closed anywhere on the screen when clicked with the mouse

Here is my code:

$(".offer-close").click(function () {
        $(".div-fix").fadeOut(500);
    });
<div class="div-fix">
<div class="offer-shop">

    <div class="cur-package">
        <div class="offer-close"><i class="fa fa-times"></i></div>
        <h6>Service</h6>

        <div class="pack-b">

            <div class="pack-price">6500 <div>
            <br>

            <div class="pack-shop"><a href="#" target="blank">Buy</a></div>
        </div>
        <div class="pack-c">any ...</div>
    </div>
</div>
</div>

For example, check out this feedback form website:

Answer №1

To resolve the issue, simply include the script provided below:

$(document).mouseup(function (e)
{
    var element = $(".fix-div");

    if (!element.is(e.target) // check if the click target is not the element...
        && element.has(e.target).length === 0) //... or any of its descendants
    {
        element.hide();
    }
});

View jsFiddle Example

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

Use a for loop to fill an array with values and then showcase its contents

I have been trying to figure out how to populate an array and display it immediately when targeting the route in my NodeJS project. Currently, I am able to console log a list of objects. However, I want to populate an array and show it when accessing loca ...

Tips for including headers with @Url.Action() to enable jwt authorization

When a user inputs correct login credentials, they should be redirected to the next page (LandingPage). However, upon clicking the Submit button, users are receiving a 401 unauthorized error instead of being directed to the next page. The JWT token is gene ...

Converting JSON data to an Excel file in an Angular application

I'm working on exporting my JSON data to an XLSX file. Despite successfully exporting it, the format in the Excel file isn't quite right. Below is the code I am using: downloadFile() { let Obj = { "data": [12,123], "date": ["2018-10- ...

How can I add an object to an array of objects in Vue.js?

Hey there! I'm new to Vue and currently working on understanding the whole concept of Vue and how to use it. Right now, my focus is on learning lists. JS: Vue.config.productionTip = false; new Vue({ el: '#app', data: { items: [ ...

Optimize the size of div elements using jQuery

I am looking for a way to minimize certain div elements on my webpage using symbols from font-awesome instead of the traditional "-" and "+". However, I am having trouble inserting the classes of the icons into my code. I attempted replacing the .html meth ...

The Media Query Menu is not aligned in the center for smaller screens, functioning in both medium and extra-small sizes

@import url('https://fonts.googleapis.com/css?family=Dosis'); * { box-sizing: border-box; } /*Xtra Small */ body { font-family: 'Dosis', sans-serif; line-height: 1.618em; font-size: 18px; background: #383231; c ...

Is it time to use JQuery post-DocumentReady?

After all the $(document).ready scripts have run, is there a way to register an event that will be executed? I have code that must run after the entire page, including jQuery scripts, has finished loading. Is this achievable? I have to register this in a ...

Organizing a JSON array and displaying it using the $.each function

My JSON array is structured like this: "series": [{ "book": 1, "chapter": 1, "title": "Title of this chapter", }, { "book": 1, "chapter": 2, "title": "Title of this chapter", }, { "bo ...

jQuery - handling dynamically added elements on page load

After incorporating certain elements into the view, I want to trigger an event. I've attempted to achieve this using the code below, but it doesn't seem to work: $(".featWrap").on("load", ".box", function() { $(".featWrap").slick({dots: t ...

jQuery for Special Characters

I am currently facing an issue with the character &gt; in my jQuery datatable. Strangely, it displays '>' correctly but when I click on a row, the character &gt; appears and I am feeling frustrated. Is there any solution to this probl ...

Converting Phone Numbers, Fax Numbers, and Zip Codes for Input into a Database

I'm facing an issue with masked inputs for Phone, Fax, and Zip Code on my webpage. The Phone and Fax numbers are currently formatted as (999) 999-9999, while the Zip Code is in the format 99999-9999. However, when I submit the page, the database requi ...

Retrieving URL from AJAX Request in Express JS

Currently, I am in the process of developing an Express App and encountering a challenge regarding the storage of the user's URL from which their AJAX request originated. In simpler terms, when a website, such as www.example.com, sends an HTTP request ...

Uploading files seamlessly without the need for refreshing the page

On my HTML page, I have a form input that allows users to upload a file. Here is the code snippet: <form action = "UploadFile.jsp" method = "post" target="my-iframe" enctype = "multipart/form-data"> <input type = "file" name = "file" ...

next-redux-wrapper is being invoked repeatedly and experiencing multiple calls to HYDRATE

I'm embarking on a new Next.js project, transitioning from a standard React app to a Next.js application. Our plan is to utilize Redux Toolkit for global state management and incorporate server-side rendering. During this process, we discovered the ne ...

Tips for personalizing PNG images within an SVG element

Looking to update the color of a PNG image within an SVG tag. The PNG images I have are transparent, and I want to customize their colors based on user selections. How can this be achieved? Is there anyone out there who can assist me? <HoodieSvg ...

Should I generate an array or pull data directly from the database?

Hey there, I've got this JavaScript app and could really use some input or tips. Here's the idea: Users log in to try and defeat a 'boss', with each player working together in the game. Let's say the 'boss' has 10 millio ...

Center the navigation links within the navigation block

I am using the startbootstrap-small-business template and customizing it to fit my requirements. I have inserted a new logo which caused me to adjust the height of the navbar to approximately 120px. Now, my goal is to vertically align the links on the righ ...

Switch between Fixed and Relative positions as you scroll

Just a small adjustment is needed to get it working... I need to toggle between fixed and relative positioning. JSFiddle Link $(window).on('scroll', function() { ($(window).scrollTop() > 50) ? ($('.me').addClass('fix ...

"What is the best way to use jQuery to create a toggle effect where one button controls the state

I'm looking to improve the efficiency of my code, but I'm not sure where to start in terms of making it more concise and organized. Any suggestions on how to streamline this code and keep it neat would be greatly appreciated. $(document).ready(fu ...

Instructions on how to determine if a client is a "desktop terminal"

So here's the deal: I have a suspicion about thin clients accessing my website. Is there a way to test if a client is a thin client without causing it to lag with JavaScript animations? I want to provide a simplified version of the site for these clie ...