jQuery replace text of element on hover
add another function to the hover event like this:
$('.btn').hover(function(){
$(this).text("I'm replaced!");
}, function() {
$(this).text("Replace me please");
});
Link for demo
$('.btn').hover(function() {
// store $(this).text() in a variable
$(this).text("I'm replaced!");
},
function() {
// assign it back here
});
This should do the trick
$(function(){
var prev;
$('.btn').hover(function(){
prev = $(this).text();
$(this).text("I'm replaced!");
}, function(){
$(this).text(prev)
});
})
$('.btn').hover(
function() {
var $this = $(this); // caching $(this)
$this.data('defaultText', $this.text());
$this.text("I'm replaced!");
},
function() {
var $this = $(this); // caching $(this)
$this.text($this.data('defaultText'));
}
);
you could save the original text in a data-defaultText
attribute stored in the node itself so to avoid variables