Need to trim all the elements in the form using javascript

Use

var allInputs = $(":input");

to get all form elements. Iterated using each function and trim it.

It will be something like this (not tested)

var allInputs = $(":input"); 
allInputs.each(function() {
        $(this).val($.trim($(this).val()));
    });

$('input').val(function(_, value) {
   return $.trim(value);
});

 $("form").children().each(function(){
this.value=$(this).val().trim();
})

will trim all textbox and textarea inside form tag but don't write unnecessary code inside form.