views:

46

answers:

4

hi im trying to add a h2 with the title of an image after each image in my page... this is what ive got so far...

not really sure where im goin wrong

$("img[title]").each(function(){
   this.after("<h2>" . this.attr(title) "</h2>");
});
+3  A: 

I think you're confusing JavaScript with php. Try:

$(this).after("<h2>" + $(this).attr('title') + "</h2>");
Kobi
A: 

ive tried the following but instead of using the title attribute it outputs the word "title"... thanks for your help btw

$("img[title]").after("<h2>" + $(this).attr("title") + "</h2>");
michael
A: 

got it..

     $("img[title]").each(function() {
  $(this).after("<h2>" + $(this).attr("title") + "</h2>");
 });
michael
Please don't litter your question with non-answers posted as answers; update your original question instead.
bzlm
This is exactly what Kobi answered, please mark his answer as accepted.
fudgey
A: 

If you want to change the attribute you have to overwrite it

.attr("which one", "what to put there")

so your code should look like that:

$("img").after("<h2>" + $(this).attr("title", "your new title") + "</h2>");

Whenever you use a jQuery function and it doesn't do what you want it to do, check the online manual, everything is explained there.

tharkun