Last active
March 31, 2017 19:58
-
-
Save imliam/de9101aa223b8363989feb320860ff10 to your computer and use it in GitHub Desktop.
jQuery - Hover Content Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
|-------------------------------------------------------------------------- | |
| 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