What is the best way to ensure that my pictures are only visible within a modal box?

I've set up a modal box called sectors with two subcategories: publication and food.

Each subcategory is linked to a div that contains an image.

My goal is to hide the image inside the modal box and display it on the screen once the box is closed.

I attempted using display:none within the div but it didn't resolve the issue.

    var content = "";

$('a[href="#ex5"]').click(function(event) {
  event.preventDefault();
  $(this).modal({
    escapeClose: false,
    clickClose: false,
    showClose: false,
  });
  $('#myDiv, #myDivs').addClass('hideme');
  $('input[type=checkbox]').prop('checked', false);
});

$('.yourCheckbox, .yourCheckboxx').change(function() {
  var elemContent = $("#" + $(this).data('target')).html();
  if ($(this).prop("checked")) {
    //$("#" + $(this).data('target')).removeClass('hideme');
    content += elemContent;
  } else {
    //$("#" + $(this).data('target')).addClass('hideme');
    content = content.replace(elemContent, '');
  }

  $("#" + $(this).data('target')).toggleClass('hideme');
});

$('[rel="modal:close"]').on('click', () => {
  $('.btn').siblings().first().html('').append(content);
  content= '';
})
.onlyThese {
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

input[type="checkbox"]+label {
  color: blue
}

input[type="checkbox"] {
  display: none
}

input[type="checkbox"]:checked+label {
  color: red
}

input:focus {
  outline: none;
}
.hideme {
  display: none;
}
.spaz {
  color: blue;
  margin-left: 40%;
}
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<!-- Remember to include jQuery :) -->

<!-- jQuery Modal -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />

<p> <a class="btn" href="#ex5">Sectors </a> <span class="spaz"></span> </p>

<div id="ex5" ; class="modal" ; style="background-color:white">

  <div style="float:left;">

    <p>
      <input type="checkbox" id="group1" class="yourCheckbox" data-target="myDiv" checked="checked">
      <label for="group1" class="onlyThese">Publication </label>
    </p>

    <div id="myDiv">
      <img src="https://tse4.mm.bing.net/th?id=OIP.qKsWIt_Qae6vtWd3-RulIQHaHa&pid=Api&P=0&w=300&h=300.jpg"> </div>
    <p>

      <input type="checkbox" id="group2" class="yourCheckboxx" data-target="myDivs" checked="checked">
      <label for="group2" class="onlyThese">Food </label>
    </p>

    <div id="myDivs">
      <img src="https://tse3.mm.bing.net/th?id=OIP.HcL9FITSR_SWsLMgMFMkYAHaEo&pid=Api&P=0&w=281&h=176.jpg"> </div>
  </div>
  <div>
    <div>
      <p style="float:right">
        <a href="#" rel="modal:close" ; class="onlyThese;"> <b>Apply</b> </a>
      </p>
    </div>

  </div>

The desired outcome is as follows:

  • Users open the modal box named sectors
  • They select a subcategory, e.g., publication
  • The content of the subcategory is loaded/fetched from the div but remains hidden inside the modal box
  • When the user clicks Apply, the modal box closes and the content is displayed

Answer №1

You should consider altering the method of creating the model.

Your JavaScript code needs to be updated in the following manner. Although it may not function as anticipated, you can adjust it to align with the events available. Feel free to ask for additional assistance if needed.

var content = "";
$('a[href="#ex5"]').click(function(event) {

  event.preventDefault();

  // hiding the images
  $('#myDiv,#myDivs').hide();

  // use the actual div instead of this, which facilitates event binding
  $('#ex5').modal({
    escapeClose: false,
    clickClose: false,
    showClose: false,

  })

// binding the before close event
  $('#ex5').on($.modal.BEFORE_CLOSE , function(event, modal) {
  // display the image before closing the modal
  $('#myDiv,#myDivs').show();
});

});

$('.yourCheckbox, .yourCheckboxx').change(function() {
  if ($(this).prop("checked")) {
    //$("#" + $(this).data('target')).removeClass('hideme');
    content = $("#" + $(this).data('target')).html();
  } else {
    //$("#" + $(this).data('target')).addClass('hideme');
    content = "";
  }

  $("#" + $(this).data('target')).toggleClass('hideme');
});


$('[rel="modal:close"]').on('click', () => {
  $('.btn').siblings().first().html('').append(content);
})
})

For more information, visit https://www.npmjs.com/package/jquery-modal#events

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

Could Knockout's value binding potentially lead to a script injection vulnerability?

My understanding is that knockout effectively deals with this scenario, but I just want to double-check. Is it correct to assume that when using the value binding with a form control such as input/textarea, there is no risk of a script injection attack? O ...

Switch from flexbox layout to begin on the next column within the same row

I am attempting to create a CSS layout where, after a specific element like :nth-child(6), the elements will break and form a separate column within the same row. The parent element should split into 2 columns after every 6th element in the same row, then ...

Filling a martial arts training center's drop-down menu with choices using an Ajax response message

Within my application, there are two drop down menus. The first drop down menu allows users to select the type of institution, and I have added an onchange event that triggers a JavaScript function to make an AJAX call in order to populate the second drop ...

Utilize Photoshop's Javascript feature to extract every layer within the currently active document

Looking for insights on a Photoshop scripting issue. I have written a solution but it's not producing the correct result. Can anyone provide feedback on what might be wrong with the code? The goal is to retrieve all the layers in a document. Here is ...

Stop the bubbling effect of :hover

How can the hover effect be prevented for the parent element when hovering over its children? Please take a look at the following code snippet: const parent = document.getElementById('parent') parent.onmouseover = function testAlert(e) { /* ...

Dynamic React animation with sliding elements

When jumping between the second and third "page" of content, I am unable to determine the reason why it is happening. The content is displayed as an absolute position. Check out the sandbox here: https://codesandbox.io/s/nostalgic-bhabha-d1dloy?file=/src ...

The path aligns with the route, yet the component designated for that route fails to render

I wrote a React component named Workout that looks like this: const Workout = props => { console.log(props); return ( <div> <h1>hello</h1> </div> ); }; export default Workout; Next, I imported this componen ...

Combining arrays using the jQuery .each() function

I'm attempting to generate an array from several div id's. Check out my code below: $(function(){ $('#content > div[id^=post]').each(function(){ var ele = Number($(this).attr('id').substr(5,4)); var arr ...

What is the material-ui component that mirrors the functionality of <input type="color"><datalist>?

I'm just starting out with javascript, react, and Material-UI, so my question (along with the code sample) might show my lack of experience. Within a Material-UI TableCell (not a form), I have the following code: <input type="color" name ...

Display my additional HTML content on the current page

Is it possible for my addon to open a predefined html page in the current window when it is started? And if so, how can this be achieved? Currently, I am able to open my addon page in a new window using the following command: bridge.boot = function() { ...

Checking the efficiency of Graphql API

Currently, I am in the process of optimizing key features within my Node.js API which incorporates GraphQL. This API serves as a proxy, receiving requests from various sources and forwarding them to multiple APIs based on the request. I am interested in ...

Conceal four input fields depending on the option chosen from the dropdown menu

I need assistance with hiding multiple input boxes. Although I've tried using if and else, it seems to only work for two of the boxes. My current code is written in regular JavaScript, but I am open to exploring a jQuery solution as well. window.onlo ...

How can I utilize the parseFloat feature within my dedicated parseFloat function located in an angular factory?

What is the solution for accessing parseFloat within an angular factory function named parseFloat? angular .module('myApp') .factory('mathService', [MathService]); function MathService() { return { parseFloat: myGl ...

"Unlock the power of Passport.js: A guide to leveraging async rendering for email templates

Currently, my setup involves running Express with Sequelize/MariaDB and Passport.js to handle user authentication. Everything seems to be working smoothly except for the activation email that needs to be sent out after a user signs up. Despite having the ...

Methods to maintain the select2 dropdown event open while interacting with another select2 dropdown

I have a situation where I need to dynamically add a class to a div whenever a select2 dropdown is open. To achieve this functionality, I am utilizing the open and close events of select2. The current code successfully adds the class when opening a selec ...

Utilizing jQuery to convert object properties into a table

Here is the table structure I am working with: <table> <thead> <tr> <th>Loan Type</th> <th>Amount Borrowed</th> <th>Current Payment< ...

Obtaining URLs from a dataset in R while handling empty values

Looking to retrieve the source and demo URLs for a collection of Jekyll themes and organize them into a data frame. library(rvest) info <- read_html("https://github.com/jekyll/jekyll/wiki/themes") data <- info %>% html_nodes(" #wiki-body li") ...

What is the best way to set a specific image as the initial image when loading 'SpriteSpin'?

I'm currently working on creating a 360-degree view using the SpriteSpin API. Everything is functioning as expected, but I have an additional request. I need to specify a specific image to be the initial landing image when the page loads. The landing ...

Convert a binary array into a signed 64-bit integer

Although JavaScript may not accurately represent all 64-bit integer numbers, it can handle numbers larger than 32 bits, which is what I am looking for. I need JavaScript to provide me with whatever level of precision it can offer. I have a byte array of a ...

Activating jquery functionality

Here is the code snippet I am working with: <script type="text/javascript> $(document).ready(function(){ $("Edit").click(function(){ $.get('fetchvalues.php'); }); }); </script> <input id="Edit" name="E ...