jQuery remove spaces on blur not working

3.4k Views Asked by At

The replace function I'm using works as I've tested it in the console but it does not work at all on blur. How can I fix that?

<script type="text/javascript">
    //Ensure spaces are not entered between tags
    jQuery(document).ready(function(){
        jQuery('#item_keywords').blur(function(){
            $(this).val().replace(/^\s+$/,"");
        });
    });
</script>
3

There are 3 best solutions below

6
Val On BEST ANSWER

you have removed the spaces but u have not assigned them any where :)

jQuery(document).ready(function(){
        jQuery('#item_keywords').unbind('blur').bind('blur',function(){
            $(this).val($(this).val().replace(/^\s+$/,""));
        });
    });
0
Dmitriy Kozmenko On

I'm using:

        jQuery(document).ready(function(){
            jQuery('#business_email').unbind('blur').bind('blur',function(){
                var a = $(this).val().trim();
                $(this).val(a + '.');
                $(this).val(a);
            });
        });
0
Richard-Xie On

You can try this.

$(document).ready(function(){
    $("#mytextbox").blur(function(){
        $(this).val($(this).val().replace(/\s/g,''));
        console.log($(this).val());
    });
});