I want a function that replace each li with an image. This is my code:
$(document).ready(function(){
var tmphref;
var tmpname;
var str = '<a href="' + tmphref + '"><img src="http://www.somesite.com/a/' + tmpname[1] + '/avatar-small.jpg /></a>';
$('#somediv li a').each(function(){
tmphref = $(this).attr("href");
tmpname = /http\:\/\/(\w+)\.somesite\.com\//.exec(tmphref);
$(this).parent().replaceWith(str);
});
});
The image is in this specific path: http://www.somesite.com/a/username/avatar-small.jpg
The code above doesn’t work. Any ideas?
Thank you in advance.
Move
after
tmphrefandtmpname, like thisbecause variable
stris already assigned values withundefinedtmphref, andtmpname, so changing the values oftmphrefandtmpnameafter that, wouldn’t effect the variablestrAnd, for that case, you don’t need to declare variable for
tmphrefandtmpnameoutside.eachfunction.