How can I replace html parts with replace()
?
<div>
<a href="http://www.google.com">google.com</a>
</div>
JS:
var e = $("div"),
fix = e.html().replace("google.com", "duckduckgo.com");
e.html(fix);
I guess html() is not working the same as text()
?
jsfiddle http://jsfiddle.net/guest271314/6rrKs/
The problem is that
.replace
only replaces first occurence. If you want to replace all occurences, you must use a regular expression with ag
(global) flag:Demo
Remember you must escape special characters such as
.
, though. If you prefer, you can useDemo
Make your pattern global by using the
g
switch:jsFiddle example
This way it replaces the link and the text.