Solved

please see what is wrong with my jquery code

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

Suggested Solutions

Title # Comments Views Activity
Angular- another way of coding hide and display 3 51
jquery - show / hide div depending on day and time 19 47
PHP Form Calculate Total Price 10 43
Javascript Form Change 5 23
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

831 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