jQuery change existing img alt tag

Sam Skirrow picture Sam Skirrow · Jul 8, 2013 · Viewed 17.4k times · Source

Simple question. I am trying to change all the image alt tags on my site using jQuery. The issue I'm having is that all my images have existing alt tags that I want to add to. Currently my code won't do anything, here is what I have placed just before closing the

<script>
$("img").attr({
alt: "This is some alt text"
});
</script>

Can someone help me figure out what is wrong here. Thanks.

Answer

Ant&#243;nio Regadas picture António Regadas · Jul 8, 2013

After jQuery 1.6 prop() is the correct one to use:

$("img").prop("alt", "This is some alt text");

http://blog.jquery.com/2011/05/12/jquery-1-6-1-released/