Loop thru each anchor and onclick display an image in modal box using jQuery

0
down vote
favorite

I have dynamically generated div's whose content contains an anchor link and path to an image.

I also have a modal box with an image tag in.

What I am trying to do is onclick of the anchor link, set the image tag path which is in the modal box.

Currently what my code does is no matter what anchor link I click, its always setting image path to the first path link.

In the link below, If you click on Test title one, you will image name 350x250 displayed in the modal box, and if you close the modal box and now click on test title two, instead displaying the image 400x350 just displays the first image.

What am I missing?

Thanks and appreciate it

Here is my JSFiddle link

JSFiddle

		<div id="coupon-listing-items" class="shopping-listings listings">
    
    <div class="left-split">
     <a class="ads-links" title="Test Title One" href="http://www.test.com/"><h2>Test Title One</h2></a><div class="coupon-text">(Get 20% of by staying 2 nights)</div>
    <span id="sLink" class="hide">http://via.placeholder.com/350x250</span>
    </div>
    
		<div class="left-split">
     <a class="ads-links" title="Test Title Two" href="http://www.test.com/"><h2>Test Title Two</h2></a><div class="coupon-text"></div>
    <span id="sLink" class="hide">http://via.placeholder.com/400x350</span>
    </div> 
    
    </div>

	<!-- Modal Box -->
  <div class="modal CouponModal">
  <span class="close-modal">&times;</span>
  <div class="modal-content">
  <input type="button" class="btnprint" name="btnprint" value="Print This Coupon" onclick="PrintC('cp-image')"/>
  <div id="cp-image">
    <img id="coupon-image" />
    <div id="caption">
	<span class="vist">Visit the website: <a id="c-weblink"></a></span>
	</div>
  </div>
  </div>
</div>

Open in new window


    $(".ads-links").each(function(index){
		
	    $(".ads-links").on("click", function(e){
        	e.preventDefault();	var imgsrc = document.getElementById('sLink')	
           $(".CouponModal").css('display', 'block'); 
		   $("#coupon-image").attr('src',imgsrc.innerHTML);
			
        });	
		$(".close-modal").click(function(e) { 
          $(".CouponModal").css('display', 'none');     
        });
		
	});

Open in new window

LVL 2
niceoneishereAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
I modified the html to make the image url an attribute of the anchor, rather than a separate element.

<div id="coupon-listing-items" class="shopping-listings listings">

<div class="left-split">
   <a class="ads-links" title="Test Title One" href="http://www.test.com/" data-image-src="http://via.placeholder.com/350x250"><h2>Test Title One</h2></a><div class="coupon-text">(Get 20% of by staying 2 nights)</div>
</div>

<div class="left-split">
   <a class="ads-links" title="Test Title Two" href="http://www.test.com/" data-image-src="http://via.placeholder.com/400x350"><h2>Test Title Two</h2></a><div class="coupon-text"></div>
</div> 

</div>

<!-- Modal Box -->
<div class="modal CouponModal">
   <span class="close-modal">&times;</span>
   <div class="modal-content">
      <input type="button" class="btnprint" name="btnprint" value="Print This Coupon" onclick="PrintC('cp-image')"/>
      <div id="cp-image">
         <img id="coupon-image" />
         <div id="caption">
            <span class="vist">Visit the website: <a id="c-weblink"></a></span>
         </div>
      </div>
   </div>
</div>

Open in new window


Then I modified the javascript to use the updated markup.

   $(".ads-links").on("click", function(e){
      e.preventDefault();
      var imgsrc = $(this).data('imageSrc');
      $("#coupon-image").attr('src',imgsrc);
      $(".CouponModal").css('display', 'block');
   });

   $(".close-modal").click(function(e){
      $(".CouponModal").css('display', 'none');
   });

Open in new window


https://jsfiddle.net/9gmqyk83/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niceoneishereAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.