4

I need to pass the data attribute in my modal. to attach with image tag in src When clicked on the link. Below is my code. I am confused. I know we can use JavaScript onclick method to pass by reference. Still can anyone give me executable code. My code is below. I am not really good in making JavaScript functions.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> <a href="#checkImage" data-toggle="modal" data-myimage="imageNamefromDataBase.jpg"> Image </a> <div class="modal fade" id="checkImage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" style="color:black;" id="myModalLabel">View Image</h4> </div> <div class="modal-body" style="color:black;"> <h1>Fee Submit</h1> <img src="#" alt="bank_chalan" id="thanks" /> </div> <div class="modal-footer"> <button type='submit' data-dismiss="modal" name='submit' class='btn btn-success'>Close</button> </div> </div> </div> </div>

1

2 Answers 2

3

you can use bootstrap modal event listener show or shown and pass the data attribute value to <img>

$('#checkImage').on('show.bs.modal', function (e) { var img = $(e.relatedTarget).data('myimage'); //alert(img); $("#thanks").attr("src",img); }); 

Working Example

$('#checkImage').on('show.bs.modal', function (e) { var img = $(e.relatedTarget).data('myimage'); //alert(img); $("#thanks").attr("src", img);	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> <a href="#checkImage" data-toggle="modal" data-myimage="http://tympanus.net/Tutorials/CaptionHoverEffects/images/1.png" class="btn btn-default"> Image </a> <div class="modal fade" id="checkImage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> </button> <h4 class="modal-title" style="color:black;" id="myModalLabel">View Image</h4> </div> <div class="modal-body" style="color:black;"> <h1>Fee Submit</h1> <img src="#" alt="bank_chalan" id="thanks" /> </div> <div class="modal-footer"> <button type='submit' data-dismiss="modal" name='submit' class='btn btn-success'>Close</button> </div> </div> </div> </div>

Fiddle

Multiple Images

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

Comments

2

If you want to include image(s) into bootstrap modal, try something like this.

<a id = 'checkImage' >Image</a> <div id = 'imagemodal' class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="myModalLabel">View Image</h4> </div> <div class="modal-body"> <h1>Fee Submit</h1> <img src="http://www.userlogos.org/files/logos/Karmody/alaTest_Iphone01a.png" id="imagepreview" > </div> <div class="modal-footer"> <button type="button" class="btn btn-info" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

In you JS file, add this

$(function() { $('#checkImage').on('click', function() { $('.imagepreview').attr('src', $(this).find('img').attr('src')); $('#imagemodal').modal('show'); }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.