With the help of JQuery, toggling the display property from block to none ensures it won't switch back to block unexpectedly

Below is a snippet of my html code:

<div class="color_area">
  <div class="blue" data-color="blue"></div>
  <div class="green" data-color="green"></div>
  <div class="red" data-color="red"></div>
</div>

<input type="checkbox" name="checkbox_click" id="checkbox_click1" value="1">
<input type="checkbox" name="checkbox_click" id="checkbox_click2" value="2">
<input type="checkbox" name="checkbox_click" id="checkbox_click3" value="3">

<div id="clickable">
  <div class="click_blue1 clickable" id="blue"><img src="images/blueone.png" id="blue"></div>
  <div class="click_blue2 clickable" id="blue"><img src="images/bluetwo.png" id="blue"></div>
  <div class="click_blue3 clickable" id="blue"><img src="images/bluethree.png" id="blue"></div>

  <div class="click_green1 clickable" id="green"><img src="images/greenone.png" id="green"></div>
  <div class="click_green2 clickable" id="green"><img src="images/greentwo.png" id="green"></div>
  <div class="click_green3 clickable" id="green"><img src="images/greenthree.png" id="green"></div>

  <div class="click_red1 clickable" id="red"><img src="images/redone.png" id="red"></div>
  <div class="click_red2 clickable" id="red"><img src="images/redtwo.png" id="red"></div>
  <div class="click_red3 clickable" id="red"><img src="images/redthree.png" id="red"></div>
</div>

And now for the jQuery part:

$('#clickable div img').hide();
$('div.clickable').css('display','none');

$( ".color_area div" ).on( "click", function() {

  var color = $( this ).attr( "data-color" );

        $( "div.clickable" ).each( function() {
            var forColor = $( this ).attr( "id" );
            if( forColor == color ) {

                if( $( this ).css('display') == 'block'){
                    $( this ).hide();
                }

                $( this ).addClass( "selected" ); 
                $( this ).css('display','block');
             }else {
                $( this ).removeClass( "selected" );
            }
        });
    });


    $( "div.clickable" ).on( "click", function() {

        if( $( this ).hasClass( "selected" ) ) {

            var color = $( this ).attr( "class" );
            $( this ).find('img').show();

             $( "." + color + " img" ).each( function() {
                 $( this ).remove();
             });
        }
    });
});

The click_red class always remains in front of div#clickable, leading to the need for toggling between display properties. I aim to select colors only once since all switch to display:block, causing the click_red class to obstruct clickability of blue and green. Any suggestions on how to resolve this issue?

Edit: To elaborate, I have an image map with 18 regions that are initially hidden. Users must first choose a color before selecting a region using the clickable area to reveal the corresponding images on the map. Essentially, it's like coloring - selecting a color highlights specific regions and displays the matching images.

Check out the fiddle here: Click Me

Answer №1

Although I've made progress, there are still some aspects that remain unclear to me. I've put together a fiddle to illustrate what I understand. If you have any additional insights, please feel free to share in the comments.

The use of .hide() and .show() instead of applying disply:none / block is highlighted.

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

Checkbox does not appear in Internet Explorer

I'm sorry, but I have never encountered this issue before. It seems that my checkboxes are not showing up in Internet Explorer but they do in Firefox. I am unsure why this is happening as I don't know of another way to create checkboxes. &l ...

Creating a thumbnail with a close button using an image

My code is available at the following link: https://codepen.io/manuchadha/pen/PBKYBJ In this form I have created, I am trying to implement an image upload feature using a file input. The goal is to display a thumbnail of the selected image below the file ...

Arrange an array of objects based on boolean values first, followed by numerical values in JavaScript

I am facing a challenge where I have an array of objects that need to be sorted based on two rules, following a specific order: Firstly, objects with the "departeYet" property set to true should come first. Secondly, the objects must then be sorted numeri ...

Adjust the initial position of the slider handle on the WooCommerce widget price filter

Is there a way to enhance the visibility of the woocommerce widget price filter by adjusting the starting position of the slider handle? For example, shifting it 10% towards the right from the beginning of the range bar. Check out the comparison of positi ...

Content is not being contained within a div container

I'm facing a unique issue that I've never encountered before - text is not wrapping inside a div element. Here's a snippet of my HTML code: http://jsfiddle.net/NDND2/2/ <div id="calendar_container"> <div id="events_container"& ...

Create multiple buttons with uniform width by adjusting padding in CSS

I recently attempted to create buttons using CSS and padding. Here is the HTML code for the buttons: <link rel="stylesheet" type="text/css" href="css/style-login.css" /> <link rel="stylesheet" type="text/css" href="css/font-awesome-4.0.3.css" /& ...

Scroll automatically to the following div after a brief break

On my website, the title of each page fills the entire screen before the content appears when you scroll down. I am interested in adding a smooth automatic scroll feature that will transition to the next section after about half a second on the initial "ti ...

What is the best way to showcase several divs, along with their respective child elements, in an inline format?

I am struggling to get 4 div sections to display side by side by using the following CSS for the parent div: .parent { display: inline; } <div class="parent"> <div class="child"> <ul> <li>t ...

Avoid re-running the onScroll function until completion

I have a unique idea for a slideshow where the slides fade in and out as the user scrolls up or down. The process involves detecting scroll movements and showing the next slide based on the direction of the scroll. The user scrolls using the scrollwheel ...

Tips for creating line breaks in Google Chart tooltips

I'm having trouble breaking a line in the code snippet below. Here is the complete code: <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script ...

Steps for resetting a div's display after adjusting the device size

I have a code that displays horizontally on desktop screens and vertically on phones. It includes an x button (closebtn) that is only displayed on phones to close the menu bar. How can I automatically display it again after resizing the page back to deskto ...

Steps for incorporating 'admin-ajax.php' on the frontend

Here is the code for Javascript. Javascript used: new AjaxUpload('#upload_btn', { action: '<?php echo admin_url("admin-ajax.php"); ?>', This function only functions when the user is logged in. ...

Spacing between products in Woocommerce product list

Welcome to my website! Check it out here: I am facing an issue with a long margin at the bottom of my woocommerce product list. I have tried using CSS to change it as shown below: .woocommerce .products ul, .woocommerce ul.products { margin-bot ...

Has $.support.fixedPosition feature been removed starting from version 1.8?

While browsing the jQuery changelog, I noticed that the $.support.fixedPosition feature introduced in version 1.7 seems to have disappeared in version 1.8. Can anyone shed some light on where it has been relocated or if it has been removed altogether? ...

Checkbox and radio buttons in JQuery UI are stacked vertically on top of each other, rather than adjacent to each other

Here is the HTML code snippet: <div id="container"> <div id="signals-summary-parameters-radio"> <fieldset> <legend>Row Filter : </legend> <label for="signals-summary-radio-all">All</label> ...

What is the best way to display elements in separate rows within an HTML table?

Hi, I'm currently retrieving data from an Excel file but it is all showing up in one row. Below is the HTML code snippet: {% block body %} # Using Bootstrap table class <table class="table table-bordered"> <thead> ...

Retrieve MySQL records based on checkbox selection status and export them to Microsoft Excel

I am trying to create a dynamic HTML form using PHP. The form includes checkboxes that, when selected, should fetch corresponding records from a MySQL database. Only the CustomerID associated with the checked checkboxes should be exported to an Excel file; ...

Is there a way to incorporate an Ajax response value into a timer function?

I am trying to create a countdown timer that retrieves its value from an Ajax call. Here is the code I have so far: function timer(seconds) { var days = Math.floor(seconds/24/60/60); var hoursLeft = Math.floor((seconds) - (days*86400)); ...

Replacing the yellow autofill background

After much effort, I have finally discovered the ultimate method to remove autofill styling across all browsers: $('input').each(function() { var $this = $(this); $this.after($this.clone()).remove(); }); However, executing t ...

Clicking on the button will instantly relocate the dynamically generated content to the top of the page, thanks to Vue

Here is some dynamically generated content in the left column: <div v-for="index in total" :key="index"> <h2>Dynamic content: <span v-text="index + ' of ' + total"></span></h2> </div> There is also a butt ...