0

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.

1
  • JS string object has a native trim method as a prototype so that you can use that no matter jQuery version has it or not. Commented Jun 19, 2021 at 4:07

2 Answers 2

0

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();
0
0

Hope you will solve this problem just modifying something like this

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.