Steps for closing a modal window when a layer is unchecked on the control panel

One of the features on my website is a modal window that pops up whenever a user selects a layer from a menu. The user can close this modal window by using the "X" icon or clicking on the map itself. However, there is an issue where if the user unchecks the layer from the menu, the modal will reappear.

I am seeking assistance in modifying the code so that the modal does not pop up again when the user unchecks the layer from the menu.

For reference, I am utilizing Leaflet for the map and Bootstrap for the modal.

Below is an example of the hypothetical code:

HTML for the modal:

<div id="myRouteModal">blah blah</div>

HTML for the custom layers menu:

<label><input id="myRoute" type="checkbox" class="check">My Route</label>

JS for custom menu control:

var overlaysMenuCtrl = L.Control.extend({ ... blah blah... });

map.addControl(new overlaysMenuCtrl)());

JavaScript:

function toggleLayer(checked, layer){
    if(checked){
        map.addLayer(layer);
    } else {
        map.removeLayer(layer);
    }
}

$(".check").change(function(){
    var layerClicked = $(this).attr("id");
//Turn layers on and off based on the ID of the radio checked
   switch(layerClicked){
    case "myRoute": toggleLayer(this.checked, myRoute);
        $('myRouteModal').modal(modalOptions);
    break;
    ...and other layers ...
}
});

Answer №1

Here is the updated code snippet:

switch(layerClicked){
    case "myRoute": toggleLayer(this.checked, myRoute);
        $('myRouteModal').modal(modalOptions);

You are currently calling

$('myRouteModal').modal(modalOptions);
every time. It would be more efficient to only call this function if the clicked checkbox is checked. To achieve this, you can modify your code like below:

case "myRoute": toggleLayer(this.checked, myRoute);
    if(this.checked) {
        $('myRouteModal').modal(modalOptions);
    }
break;

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

Switch directions following a successful status code of 200

After successfully logging in (with a status of 200), I want to change my route to /expenses. async SendLogin() { const response = await axios.post("api/auth/login", { email: this.email, password: this.password, }); localStorage.setItem("toke ...

Tips for managing the 'undefined' error in JavaScript when dealing with constantly changing deeply nested objects

Currently, I am developing a project using React with Formik for form handling and Yup for validation schemas. My current task involves validating a nested form to add a CSS class to the input if there is an error in the respective index of the nested fiel ...

Change the JavaScript code to output HTML rather than just plain text

I've created a small plugin for tinymce4 that adds an additional dropdown menu with a list of headers (e.g. h1, h2...). The issue I'm facing is that I'm trying to display these header elements with their corresponding styles (e.g. <h1> ...

What is the best way to match the minimum height of one div to the height of another div?

I need to dynamically set the minimum height of #wrapper equal to the height of #sidebar, but the height of #sidebar is not fixed. Here is my attempt at achieving this with JavaScript: var sidebarHeight = jQuery('#sidebar').height; jQuery(&apos ...

Tips on invoking a factory method from a different service method in AngularJS

I have a factory method that goes like this.. (function (angular) { "use strict"; angular .module('app') .factory('UserService', ['$rootScope', '$q', function ($rootScope, $q) { ...

What is the best way to remove elements in an array based on another array?

Is there a way to remove elements from one array that are present in another array? I currently have an array ["a", "b", "c"] as my first array. And the second array consists of [["a", "e"], ["e", "b", "c"], ["a","c"]]. How can I remove elements from the ...

Display or conceal objects within a 3D model with an HTML checkbox

I have utilized JavaScript, CSS, and Three.js to create a 3D model. The project comprises a total of 5 JS files, 1 CSS file, and 1 HTML file. Here is an overview of the model: [] Showers have been incorporated for each cubicle, which should only be vis ...

Incorporate HTML and JavaScript to dynamically show a button when a specified condition evaluates as true and hide the button if the

I need help with a scenario where I want to show a button only when a specific variable reaches a certain value. For instance, if the variable equals 5, the button should be displayed; otherwise, it should be hidden. Here are the two buttons included withi ...

When attempting to use Ajax, the operation fails; however, if I directly access the URL,

When using firebug, I attempted to make the following AJAX call: var rootUrl = 'http://172.24.105.22:8080/geoserver/Chennai_Sub/ows'; var defaultParameters = { service: 'WFS', version: '1.0.0', request: 'Get ...

Spicing up javascript with Currie's arrow function

Can you please break down this function in a straightforward way (without using arrows)? I'm having trouble understanding if "foo" in Promise.resolve "foo" is an argument or a callback function. module.exports = function foo(req, res, next) { retu ...

experiencing a problem with the older version 1.5 of the swfobject.js software

Hello there! I am encountering an issue with swfobject.js version 1.5 specifically in IE7 and IE8. My chart is not displaying in these web browsers, but it works perfectly fine on Chrome, Firefox, and even IE 6. Do you have any suggestions on how to resolv ...

Creating dynamic dropdown menus with added options

My goal is to integrate the names of countries along with their flags in a dropdown menu. I am utilizing the select2 library for this purpose. However, despite displaying the country names in the dropdown, the width of the dropdown is too narrow to prope ...

Is there any need for transpiling .ts files to .js when Node is capable of running .ts files directly?

If you are using node version 12, try running the following command: node hello.ts I'm curious about the purpose of installing typescript globally with npm: npm install -g typescript After that, compiling your TypeScript file to JavaScript with: ...

Add content to div element using input from textarea

I'm attempting to create a basic text editor where users can input text and by clicking the edit button, it will add that text to the end of the id="textArea". However, nothing happens when I click the edit button after entering text in the textarea. ...

Creating a dynamic multi-select feature in AngularJS with ng-repeat

I'm relatively new to AngularJS and JavaScript, but I've managed to create a functional multi-select list. The ng-model linked to the dropdown is part of a "user" DTO object, specifically a property that stores an array of "groups" the user can j ...

Is there a way to interact directly with the threejs components in React without using react-three-fiber?

I am looking to access my 3D model element and control the camera, mesh, animation, etc. from another component in a project where I am using pure threeJs and React. The pure threejs code is added in the componentDidMount part of the React App class, and n ...

Error: Call stack limit reached while passing JSON data using Node.js

I am utilizing the ajax-request module to send Json data using a post request in my Node.js application. Below is the relevant code snippet: logMessage : function(url, message, next) { var d1 = message["sender"]; var d2 = { id: message[sender"]["id"], ...

"Utilizing Express.js and PHP for Streamlined Functionality

I have been working on a project that involves using expressjs, Php, Html, and MongoDB. Here is the code snippet from my "Index.Html" File: <form action="/Login" method="POST"> <input type="text" placeholder="name" name="Username"> <in ...

What is the best way to effectively adjust the code structure in a Node.JS project?

[Summarized] Focus on the bold parts. Although I am relatively new to Node.JS, I have been able to successfully build some projects. However, I have come across a burning question that has left me frustrated after searching Google for answers without much ...

"Variables declared with const do not get hoisted when used in immediately invoked functions

Recently, I was experimenting with the new ECMASCRIPT-6 const keyword. There was one particular behavior of the keyword that left me confused. Imagine we have two functions: In the first case: (function(){ console.log(_t); const _t=10; })(); and i ...