I can’t get this simple .replaceWith() to co-operate with a .hover(). Would appreciate some help.
I want it the text to change to EXPAND when I hover on it, and go back to SHOWS when I mouseout. I can get the text to change to EXPAND, but I can’t get the text to go back to the original, SHOWS.
The JSFiddle code for anyone kind enough to have a look.
$(document).ready(function() {
$("p").hover(
function () {
$('#shows').replaceWith('<h2>Expand</h2>');
},
function () {
$('#shows').replaceWith('<h2>Shows</h2>');
}
);
});
The problem is when you’re replacing it it’s losing the
idattribute. So although themouseoutfunction of thehover()event fires it can’t find the element#shows.Try this instead:
You should always use
.text()or.html()in examples like this because acctually all you want to do is change the inner bit the TEXT or the HTML contained within the<h2>tag, rather than the whole element.If you want to use replaceWith: