Dynamically Open Image in Modal Bootstrap

Asked

Viewed 7,627 times

5

My intention is the following, after generating the list of images by PHP, I wanted that when I clicked on some of the images it opened in a modal dynamically, where I would pass the address of the image by some parameter and it opened in the modal of bootstrap/jquery.

EX:

<a href="#" parametro="imagem1.jpg" id="abrirModal" ><img src="imagem1.jpg" /></a>
<a href="#" parametro="imagem2.jpg" id="abrirModal" ><img src="imagem2.jpg" /></a>
<a href="#" parametro="imagem3.jpg" id="abrirModal" ><img src="imagem3.jpg" /></a>
<a href="#" parametro="imagem4.jpg" id="abrirModal" ><img src="imagem4.jpg" /></a>

Someone can help me?

  • a tip: id never can repeat Diego, id is the same identity, there can only be one for each.

1 answer

2


And very simple, just include the modal, which you can find in the bootstrap documentation and exchange the id’s for class as mentioned by Guilherme Nascimento.

After that you will need an event to open the modal, and take the image url and set in the modal img.

Example;

$(".abrirModal").click(function() {
  var url = $(this).find("img").attr("src");
  $("#myModal img").attr("src", url);
  $("#myModal").modal("show");
});

See working on jsfiddle

If there is any delay in showing the image you can put inside the event 'show.bs.modal', more details can be found in the documentation itself http://getbootstrap.com/javascript/#modals

  • It worked perfectly, thank you very much! Valew. I’m a javascript/jquery layman. thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.