I would like the "alt" to be displayed when hovering over the image. Everything works fine, except that although the photo has "alt" without the "-" and ".jpg" signs, the "alt" of the photo is displayed with these signs. When I edit these codes in "codepen" everything works ok but after uploading the codes to the page they don't work as they should.The CSS code is ok. It's about helping with JS.
<img class="img" src="https://photoooo777.com/Some-Photo.jpg" alt="Some Photo" width="200" height="300">
<p class="alt">Some-Photo.jpg</p>
$(".img").wrap('<div class="alt-wrap"/>');
$(".img").each(function () {
$(this).after('<p class="alt">' + $(this).attr("alt") + "</p>");
});
$(document).ready(function () {
$("img").each(function () {
var $img = $(this);
var filename = $img.attr("src");
if (typeof attr == typeof undefined || attr == false) {
var altText = filename.substring(
filename.lastIndexOf("/") + 1,
filename.lastIndexOf(".")
);
altText = altText.replace("-", " ").replace(".jpg", "");
$img.attr("alt", altText);
}
});
});
Please help me edit these codes so that the name is displayed on hover, e.g. "Some Photo" instead of "Some-Photo.jpg".
You can remove the line where you are setting the
altattribute of the image to the file name by removing this line:Also you don't need to wrap the images in a div with class
alt-wrapand add a newpelement with classaltafter the image.Instead, you can use the
titleattribute to show the text on hover over image. You can set thetitleattribute to the same value as thealtattribute on each image:This way you don't need to create any new elements and you can remove the wrapping and the
pelement.