the following lines of code are supposed to take whatever i type in and replace ” ” with “-” which works on the first space, but no space thereafter.
$('#myinput').live('keypress', function() {
var value = $(this).val();
value = value.replace(" ", "-");
$('#mydiv').text(value);
});
try
value = value.replace(new RegExp("\\s", "g"), "-")You can also use
"\\s+"if you want to replace multiple consecutive spaces with a single-
Also look into
inputas an event and.delegateas a method.Relying on
RegExp,.delegate,input