Sandeep Shawn Tripathy Sandeep Shawn Tripathy - 2 months ago 12
jQuery Question

Add query string to Image Source(img src) if it has a particular class or ID

I want this in either PHP or JS. Its like, if img has particular class them a query string will be appended to its source url. Lets assume, the particular class is 'hello' & query string is '?source=fb'
So, if img has class 'hello' -

<img class="hello" src="http://example.com/1.jpg?source=fb">


The PHP should check & append it to the img src.

Any function that can do this?

Answer

$('img.hello').attr('src',$('img.hello').attr('src')+"addedstring")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="hello" src="http://example.com/1.jpg?source=fb">

Use like this