/    Sign up×
Community /Pin to ProfileBookmark

Multiple images with title attribute tag – display them on a single DIV on hover

Hi,

I’m new to jquery and javascript so sorry for asking such a simple question.

Link: https://aturma.pt/picl/

On this homepage each image has a unique title atrribute tag. The idea is that when a user hover’s a image, it’s title attribute tag displays inside “span.title”. When the user’s cursor leaves the image, it prints back “production & art direction”.

At this moment all images on hover are displaying only the first image title. Can someone please tell me what’s wrong with my code?

“`
$(function() {
$(‘img.pic’).hover(function() {
var title = $(this).attr(‘title’);
$(‘span.title’).text($(“img.pic”).attr(“title”));
}, function() {
$(‘span.title’).text(“production & art direction”);
});
});
“`

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 07.2021 — This:

`$("img.pic").attr("title")</C><br/>
returns the title of the first image only. Instead use $(this) which is the image currently being hovered:<br/>
<C>
$('span.title').text($(this).attr("title"));`
Copy linkTweet thisAlerts:
@francisknightleyauthorSep 07.2021 — @Sempervivum#1636644 Works like a charm. Thank you so much!
×

Success!

Help @francisknightley spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...