Sam Skirrow
Sam Skirrow

Reputation: 3697

jQuery change existing img alt tag

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.

Upvotes: 3

Views: 20506

Answers (3)

Sergio
Sergio

Reputation: 28837

Your code should work, but you should use prop like this instead $("img").prop('alt': 'This is some alt text');.
You might be missing this inside you <head> tags to actually load jQuery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  • Keep in mind that what you want will change alt to all images...

Upvotes: 2

Ant&#243;nio Regadas
Ant&#243;nio Regadas

Reputation: 724

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/

Upvotes: 8

user2549616
user2549616

Reputation:

You can try this:

$(function() {
   $('img').attr('alt', 'Some Alt Text Here');
}

Upvotes: 4

Related Questions