Solved

please see what is wrong with my jquery code

Posted on 2012-04-07
1
187 Views
Last Modified: 2012-04-22
Hi,

I would have expected the code below to have changed the alt tags on the imgs however when I use firebug to look at the code I see that the alts remain unchaged.


<html>
      <head>
            <title>This is a test</title>
            <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
            <script type="text/javascript">
                  $('img').each(function(n) {
                        this.alt='This is an image['+n+'] with an id of '+this.id;
                  });
                  
            </script>
            </head>
            <body>
                  <img id="myImage" src="image.gif" alt="An image" class="someClass" title="This is an image" />
                  <img id="myImage2" src="image2.gif" alt="An image2" class="someClass2" title="This is an image2" />
            </body>
</html>
0
Comment
Question by:jecommera
1 Comment
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 37819610
Here you go.
<script type="text/javascript">
	$(document).ready(function() {
		$('img').each(function(n) {
			$(this).attr('alt', 'This is an image['+n+'] with an id of '+this.id);
		});
	});
</script>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now