Search code examples
jquerysrc

add to src in img with jquery


i need to change this :

<img src="example.jpg" alt="">

into this:

<img src="example.jpg?width=500" alt="">

with jquery, how can this be achived, because i don't want to overwrite the src, but just add that ?width=500 bit.

my code looks something like this in total:

<div class="img-wrap"> 
     <p><a href="link">Link to something</a></p>

     <img src="example.jpg" alt=""> 

</div>

EDIT Okey, im sorry i did not specify, but i cannot make any changes to the img tag. The image is generated from the backend. the div, is the only thing i have any sort of power over.


Solution

  • Try this

    <script>
      $(".img-wrap p img")[0].src += "?width=500";
    </script>
    
    <div class="img-wrap">
      <p><a href="link">Link to something</a>
      </p>
      <img src="example.jpg" alt="">
    </div>