Solved

please see what is wrong with my jquery code

Posted on 2012-04-07
1
185 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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now