0

Couldn't find a clear answer to what I'm trying to achieve. I'm sure it's simple but I'm missing it.

I will be building a list of links on a page that all have the class of "prettyLink" and each will have a title. On mouseover I'm trying to store that title in a variable and remove the title, then on mouseout replace that title with what was stored. (basically removing the title while being moused over and putting it back when moused out)

here is the code:

$('a.prettyLink').mouseover(function() {
   var oldTitle = this.title;
   $(this).removeAttr('title');
}).mouseout(function() {
   $(this).attr('title',oldTitle);
});

Any help is appreciated. Thanks!

8 Answers 8

6

Simply declare the variable outside of the mouseover() method:

var oldTitle;
$('a.prettyLink').mouseover(function() {
   oldTitle = this.title;
   $(this).removeAttr('title');
}).mouseout(function() {
   $(this).attr('title',oldTitle);
});

JS Fiddle demo.

Declaring the variable outside of the method allows the variable's value to be assigned within the method, and have that new value be available elsewhere.

Sign up to request clarification or add additional context in comments.

6 Comments

Darn, you beat me with the exact same answer by 20 seconds. +1
@minitech: muahahahahaaaaaa...! cough Sorry... =D
+1 for content: attr(title); in a:after in your jsFiddle's css. I did not know you could do that.
Crap... can I choose more than one answer?? Sorry minitech... =(
There could be problems with this approach if more than one element match the selector $('a.prettyLink'). If two of those fire a mouseover event before a mouseout event fires, one of them will clobber the other's oldTitle value.
|
3

You need to save oldTitle outside of the local scope. I like to use jQuery's .data() method.

$('a.prettyLink').mouseover(function() {
   $(this).data('oldTitle', this.title);
   $(this).removeAttr('title');
}).mouseout(function() {
   $(this).attr('title',$(this).data('oldTitle'));
});

Comments

2
var oldTitle;
$('a.prettyLink').mouseover(function() {
   oldTitle = this.title;
   $(this).removeAttr('title');
}).mouseout(function() {
   $(this).attr('title',oldTitle);
});

Should work.

1 Comment

THANK YOU!!! Nice and clean and works perfectly. Thanks everyone for your input.
1

You can use jQuery.data to store the information and retrieve it later

Comments

1

It would be better if you use hover event:

var oldTitle;
$('a.prettyLink').hover(function() {
   oldTitle = this.title;
   $(this).removeAttr('title');
},function() {
   $(this).attr('title',oldTitle);
});

That's because mouseover triggers many times, hover only once it enters (equivalent to mousenter)

Hope this helps. Cheers

Comments

0

You could try placing a hidden <span id="title"></span> on the page, which will hold that variable and you're always just setting/getting from that span.

Then use your code:

$('a.prettyLink').mouseover(function() {
   var oldTitle = this.title;
   $('#title').val(oldTitle);
   $(this).removeAttr('title');
}).mouseout(function() {
   $(this).attr('title',$('#title').val());
});

Another way would be to declare oldTitle as a global in your page and use it that way.

2 Comments

placing it in a hidden input is a last resort. I'm trying to avoid that if I can
I agree, just another way of doing it :)
0

Consider using data(): http://api.jquery.com/jQuery.data/

This has the advantage of storing the value with the element that the events are bound to, whereas a declared global variable might get overwritten by events from other elements that do the same thing.

1 Comment

I tried using data in the function but it still didn't seem to work. i.e. function(data) {
0

Here my sollution

$(".swap").mouseover(function () {
    var $img = $(this).find('img');
    $(this).data('oldSrc', $img.attr('src'));
    $img.attr("src",  $img.data('alt-src'));
}).mouseout(function () {
    var $img = $(this).find('img');
    $img.attr("src", $(this).data('oldSrc'));
});

<div class='swap'><img src='mysrc' data-alt-src='new_src'/></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.