Skip to content

Instantly share code, notes, and snippets.

@westonwatson
Created October 23, 2012 19:34
Show Gist options
  • Save westonwatson/3941055 to your computer and use it in GitHub Desktop.
Save westonwatson/3941055 to your computer and use it in GitHub Desktop.
Possible update for auto-title-tooltip
var autoTitleTooltip = function(){
var elementType = 'div';
var customClass = '';
domIdFromString = function(myStr){
myStr+=Math.random();
myStr=myStr.toLowerCase();
myStr=myStr.replace(/ /g,"");
myStr=myStr.replace(/[^a-zA-Z0-9]+/g,"");
return myStr;
}
this.undo = function(){
jQuery('.auto-title').each(function(){
currentId = jQuery(this).attr('data-title-id');
if (typeof currentId == 'string'){
currentId = '#' + currentId;
jQuery(this).attr('title',jQuery(currentId).text());
jQuery(currentId).remove();
jQuery(this).off('mouseover').off('mouseleave');
}else{
alert(typeof currentId + jQuery(this).html());
}
});
}
this.convert = function(targetSelector){
targetSelector = targetSelector ? targetSelector : 'a';
jQuery(targetSelector).each(function(){
var currentName = "";
//current title
var currentTitle = jQuery(this).attr('title');
if (typeof currentTitle == 'string' && currentTitle != ''){
//unique id built from title
currentName = domIdFromString(currentTitle);
//insert new div below target link
jQuery(this).after(
//build new div to insert
jQuery('<'+elementType+'>')
.attr('id',currentName) //set dynamic/unique id
.addClass(customClass) //custom css/class
.html(currentTitle) //title content
.css({'display':'none'}) //hide
).addClass('auto-title');
//store dynamic title id
jQuery(this).attr('data-title-id',currentName);
//mouse enter
jQuery(this).mouseover(function(){
//reference dynamic title id
var myId = '#'+jQuery(this).attr('data-title-id');
jQuery(myId).fadeIn('slow');
});
//mouse out/leave
jQuery(this).mouseleave(function(){
var myId = '#'+jQuery(this).attr('data-title-id');
jQuery(myId).fadeOut();
});
jQuery(this).attr('title',''); //clear regular title
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment