0

I'm gettig value of img from views.py now, I want that when user click on the image, it should open in a modal.

  1. index.html image sections:
<!-- Card body -->
<div class="card-body">
   {% for val in data.list %}
   <img src="{{val.Img}}" class="img-thumbnail pop_img">   -------here i'm getting the image properly and the path is showing correctly
   {% endfor %}
</div>

  1. index.html modal section
<div class="modal fade" id="imagemodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <img src="" class="imagepreview" style="width: 100%;" >
      </div>
    </div>
  </div>
</div>
  1. main.js
$(function() {
        $('.pop_img').on('click', function() {
            var img = $(this).find('img').attr('src')
            console.log(img)         -----------------but on console it prints : undefined 
            $('.imagepreview').attr('src', $(this).find('img').attr('src'));
            $('#imagemodal').modal('show');
        });
});

and while inspecting on elements, it shows <img src(unknown)> inside modal.

How can I get the value of src attr value and place it on modal?

1 Answer 1

1

The .find() function on this line returns all the img elements in the class, var img = $(this).find('img').attr('src'). This problem can be fixed by using .first() instead. So var img = $(this).first('img').attr('src')

Sign up to request clarification or add additional context in comments.

1 Comment

how it is taking the src of the image i clicked. i thought it will choose the first src between all img tag. i thought if i have 10 src it will choose the 1st one everytime but this is not so.why it is choosing the current src which image have been clicked ? it woked fine. nice and thanx for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.