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

后端 未结 3 858
离开以前
离开以前 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:51

    Try this:

    function trim(el) {
        var trimmedValue = el.val().trim();
        el.val(trimmedValue);
    }
    

    Your syntax for .trim() is wrong. To trim a input's value you don't need any regex, just try to change this:

    $(.input).text().trim();
    

    to this:

    $('input').val().trim();
    

    input is not a class, so you don't have to put the dot before it. Then you have to include it inside ' '. The value of an input is given by val() method, not text().

    This will only trim the input's value, you have to use this inside a function in order to make it works properly, as in the example above.

提交回复
热议问题