I ask the question again because with all the answers they gave me I still couldn't solve the problem, which also seems super strange to me because several of the answers have a lot of logic!
What I need?
Open a modal when you click on an image and then have the modal close only when you click outside of it.
What do I use?
Materialize and JQuery and Thymeleaf (but shouldn't affect anything)
HTML code:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" th:href="@{/favicon_heladeria.png}" />
<link rel="stylesheet" th:href="@{/vendor/materialize/css/materialize.css}" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<link rel="stylesheet" th:href="@{/app.css}" />
<h2 class="mid-title">Nuestros Helados</h2>
</div>
<div class="helados container">
<div class="row">
<div class="col s12 l4" id="chocoDiv">
<a class="modal-trigger" href="#chocolateModal">
<img class="tastes" th:src="@{/images/chocolate.png}" />
</a>
<span class="taste-name">CHOCOLATES</span>
</div>
<div class="col s12 l4">
<img class="tastes" th:src="@{/images/crema.png}" />
<a href="#"></a>
<span class="taste-name">CREMAS</span>
</div>
<div class="col s12 l4">
<img class="tastes" th:src="@{/images/dulceDeLeche.png}" />
<a href="#"></a>
<span class="taste-name">DULCE DE LECHES</span>
</div>
<div class="col s12 l4">
<img class="tastes" th:src="@{/images/frutales.png}" />
<a href="#"></a>
<span class="taste-name">FRUTALES</span>
</div>
</div>
</div>
<!-- Modal -->
<div id="chocolateModal" class="modal">
<div>
<div class="modal-content">
<h4 class="modal-title">Chocolates</h4>
</div>
</div>
</div>
<script th:src="@{/vendor/jquery/jquery-1.11.3.js}"></script>
<script th:src="@{/vendor/materialize/js/materialize.min.js}"></script>
<script th:src="@{/app.js}"></script>
</body>
</html>
I leave the comments so you can see the other options I tried without achieving anything useful :(
JS code:
$(function(){
// Activate mobile nav
$(".button-collapse").sideNav({edge: 'right'});
});
// $(document).ready( () =>{
// $('.modal').modal();
// });
// ESTE FUE EL ÚNICO QUE ANDUVO (Pero luego no lo puedo cerrar)
//$('#chocoDiv').on('click', () => {
// $('#chocolateModal').show();
//});
document.querySelector('#chocoDiv').addEventListener('click', () => {
var element = document.querySelector('#chocolateModal');
var modal = M.Modal.init(element, {});
modal.open();
});
//$(document).ready(function(){
// $('.modal').modal();
// });
Any idea what I may be doing wrong? It is rare that neither option works well!
You don't need
#modalOut
materialize modals they have an option which isdismissible
. What this option does is that when you click outside the modal it closes. Its default value is True . You can see the documentation here . I recommend working with instances of the plugin. That doc also shows you how to do it. It makes it easier for you to work with events in the future.I leave you an example that I hope is what you were needing. Cheers!
Edit with option 2 using the asking user's code:
This is very important. I think part of your mistake is not initializing .modal
Try with this.
You just have to specify that this id is a modal.
According to the documentation to open a Materialize modal window is:
This would be the code using Jquery, where to make reference to open the modal by the class. You also have the full version:
To close it would be:
I hope it helps you.
I suggest you review the documentation !!
Documentation-Modal
Maybe this will work for you: in this example the word Hello opens the modal via "Om.onclick" , Om being the Id of P.
It works for me with this:
When you click on the button you create an instance of the modal and pass the html element with class
.modal
and the options as a parameter. Then call the methodopen()
and it should work.As they said, by default the modal is dismissible, which means that it will be closed when you click outside or with the key ESC.
According to your approach it would be like this:
With this you open the modal click on the image and it closes when you click outside of it. I hope it helps you