1

I'm trying to get the correct src of the html5 picture tag:

<picture>
<source media="(max-width: 575px)" srcset="/fileadmin/_processed_/7/d/csm_25675374538_e92c22790b_k_0fae925c97.jpg">
<source media="(max-width: 767px)" srcset="/fileadmin/_processed_/7/d/csm_25675374538_e92c22790b_k_b6fa9f6d7f.jpg">
<source media="(max-width: 991px)" srcset="/fileadmin/_processed_/7/d/csm_25675374538_e92c22790b_k_2c0e132222.jpg">
<source media="(max-width: 1199px)" srcset="/fileadmin/_processed_/7/d/csm_25675374538_e92c22790b_k_ad9b3a0b15.jpg">
<!-- FALLBACK -->
<img class="highlight-image" src="/fileadmin/user_upload/25675374538_e92c22790b_k.jpg" alt="" width="1920" height="1280">
</picture>

When I try the following, than I get the path of the fallback image

$('img.highlight-image').attr('src')

But I want the src of the img which is at the screen.

Rob
  • 14,746
  • 28
  • 47
  • 65
Ralf
  • 836
  • 1
  • 9
  • 32

2 Answers2

1

Hope this works for you.

$(document).ready(function() {
    const img = $('img.highlight-image');
    console.log(img[0].currentSrc); // returns the current shown image
});
Peter Bode
  • 222
  • 1
  • 9
0

your code looks fine, but it returns a array so you can try to use

$('img.highlight-image').first().attr('src')
Lucas
  • 275
  • 8
  • 37