I have the following little whitespace clean up function and while it works perfectly when losing focus by tabbing to the next tabindex, if I simply lose focus by clicking off the element that is currently receiving focus and re-examine the clean up is done EXCEPT for the [ \t]+$ at the end of the “string”
function cleanUpYourAct(){
$(':text').each(function(index){
var messy = $(this).val();
var clean = messy.replace(/\s+/g," ");
var trimmed = clean.replace(/^[ \t]+|[ \t]+$/,"");
$(this).val(trimmed);
});
}
$(':text').blur(cleanUpYourAct);
Any ideas? Maybe regex set I bunched together is garbage?
Thank you
You’re not doing a global replace on the second regex, so if there is whitespace at the beginning you’re not also replacing whitespace at the end:
EDIT:
You might also think about performing this action on just the element that is the event target.
Something like: