Skip to content

Instantly share code, notes, and snippets.

@imliam
Last active March 31, 2017 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imliam/de9101aa223b8363989feb320860ff10 to your computer and use it in GitHub Desktop.
Save imliam/de9101aa223b8363989feb320860ff10 to your computer and use it in GitHub Desktop.
jQuery - Hover Content Change
<button class="btn btn-primary" data-hover-content="Goodbye world">
Hello world
</button>
<button class="btn btn-primary" data-hover-element="#elementContent">
Button Content
</button>
<div id="elementContent" style="display:none;">
Element Content
</div>
/*
|--------------------------------------------------------------------------
| jQuery - Hover Content Change
|--------------------------------------------------------------------------
|
| Simple jQuery script to change the inner content of an element in the
| DOM to an attribute or other element's value.
|
*/
$('[data-hover-content], [data-hover-element]').on('mouseover', function() {
$(this).attr('data-original-content', $(this).html());
if ($(this).attr('data-hover-content')) {
$(this).html($(this).attr('data-hover-content'));
} else if ($(this).attr('data-hover-element')) {
var selector = $(this).attr('data-hover-element');
var content = $(selector).html();
$(this).html(content);
}
}).on('mouseleave', function() {
if ($(this).attr('data-original-content')) {
$(this).html($(this).attr('data-original-content'));
$(this).removeAttr('data-original-content');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment