javascriptjqueryjquery-3jquery-1.7jquery-migrate

$.trim().val is not a function


I am working on updating jQuery version of our applications from jquery version 1.7 to 3.6 so, I am facing issue with 'trim' method. we have used trim method like in old jQuery version but it has changed in latest jQuery 3.6 so, I have modified it using accordingly to 3.6 but still it is showing error as '$.trim(...).val is not a function'

my old jQuery script(1.7):

var my_variable1 = $("#my_variable1_id").val().trim();

my new jQuery trim method is (3.6):

var my_variable1 = $("#my_variable1_id").val() === null ? '' : $.trim("#my_variable1_id").val();

Any help would be appreciated, Thanks.


Solution

  • There should be no reason to use $.trim. As the docs put it:

    Note: This API has been deprecated in jQuery 3.5; please use the native String.prototype.trim method instead.

    Take the value, alternate it with the empty string, and trim - not with jQuery, but with plain JavaScript (which was introduced around a decade ago and should be usable everywhere).

    var my_variable1 = ($("#my_variable1_id").val() || '').trim();