How can trim spaces in all inputs without adding methods or classes?

后端 未结 3 876
离开以前
离开以前 2021-01-07 07:29

I\'m trying to remove blank spaces from the begining and ending of inputs in general without adding a class or id or event

I tried this live demo but is using

3条回答
  •  粉色の甜心
    2021-01-07 07:34

    try $.trim on change input with type text:-

    jQuery

    $(function(){
        $('input[type="text"]').change(function(){
            this.value = $.trim(this.value);
        });
    });
    
    
    

    Search1:

    Search2:

    Search3:

    Search4:

    Search5:

    Vanilla

    window.onload = function() {
      var inputs = document.getElementsByTagName('input');
      for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'text') {
          inputs[i].onchange = function() {
            this.value = this.value.replace(/^\s+/, '').replace(/\s+$/, '');
          };
        }
      }
    }

    Search1:

    Search2:

    Search3:

    Search4:

    Search5:

提交回复
热议问题