Javascript to remove spaces from .value

//loop thought all the sku field
$('.optionSku').each(function() {

    //if found field are empty
    if (jQuery.trim(this.value) == "") {

        //loop thought all the name field to copy to the empty field
        $('.optionName').each(function() {

            if ($(this).closest("tr").find(".optionSku").val() == "") {

                empty++;
                $(this).closest("tr").find(".optionSku").val($(this).val());
            }

        });
    }
})

How to remove space from this.value using JQuery? I try to put if (jQuery.trim(this.value) == ""), but it cannot remove the inline space and removes the leading and trailing spaces only.

Answer

this.value =  this.value.trim(); 
//you need to assign the trimmed value back to it.

Note that trim may not be available in IE < 9 version. So you can use:

this.value = $.trim(this.value);

Leave a Reply

Your email address will not be published. Required fields are marked *