Can somebody tell me what is wrong with this code:
$("#replace").text().replace("cat","dog");
Use callback function for .text():
function Type: Function( Integer index, String text ) => String A function returning the text content to set. Receives the index position of the element in the set and the old text value as arguments.
$("#replace").text(function(i,oldtext){
return oldtext.replace("cat","dog");
})