Suppose I have the following HTML:
I would like to switch the src to bar2.jpg>
src
bar2.jpg>
Just do .attr('src', 'foo') because you're assigning a src regardless. Only remove the attribute if you don't need it entirely.
.attr('src', 'foo')