Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select an image inside of a div to change its 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.

like image 388
leora Avatar asked Dec 09 '11 02:12

leora


2 Answers

$('.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.

  • .children(): http://api.jquery.com/children
  • .attr() : http://api.jquery.com/attr

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

like image 75
Jasper Avatar answered Sep 21 '22 17:09

Jasper


$('div.event img').attr('src', '/anything');

If you have several divs with event as the class you're better off selecting the div by id if you don't want to change the source of all the images

like image 41
Serge Avatar answered Sep 24 '22 17:09

Serge