How can I select an image inside of a div to change its source?

leora picture leora · Dec 9, 2011 · Viewed 75.5k times · Source

I have the following div and I know the selector Id of the DIV.

 <div class="event"><img src="/Content/Images/Icons/calendar16.png">Event Name</div>

but I don't know, what the image is. I need something to find the image selector inside the div, that I have. So I can go change the source of the image to a new image.

Answer

Jasper picture Jasper · Dec 9, 2011
$('.event').children('img').attr('src', '<source here>');

This selects all the elements with the event class and then finds their children img elements. If you have multiple matches and want to change their sources differently then you can use $.each() to iterate through them.

A demo: http://jsfiddle.net/aPzgR/