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>");
});
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>");
});
I think you're confusing JavaScript with php. Try:
$(this).after("<h2>" + $(this).attr('title') + "</h2>");
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>");
got it..
$("img[title]").each(function() {
$(this).after("<h2>" + $(this).attr("title") + "</h2>");
});
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.