I have a search input box on a site. If the user enters text in the input box I like to replace all [space]-characters with ' & ' (in words: space&space)
But I have a small mistake in my code because if the user tries to delete text (backspace) then it inserts a ' & ' again if the last char was a replaced space...
See my example on JSFiddler: http://jsfiddle.net/JFEb4/
Any ideas?
Code:
$("[name='search']").keyup(function() {
var input = $(this).val().replace(/ & /g, "x");
if (input.indexOf(" ") >= 0) {
$(this).val(input.replace(' ', ' & ').replace(/x/g, " & "
});