How do you automatically set text box to Uppercase?

后端 未结 12 601
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 23:50

I am using the following style attribute to set the user input to uppercase so that when the user starts typing in the text box for example railway, then it should

12条回答
  •  礼貌的吻别
    2021-01-30 00:34

    Try below solution, This will also take care when a user enters only blank space in the input field at the first index.

    document.getElementById('capitalizeInput').addEventListener("keyup",   () => {
      var inputValue = document.getElementById('capitalizeInput')['value']; 
      if (inputValue[0] === ' ') {
          inputValue = '';
        } else if (inputValue) {
          inputValue = inputValue[0].toUpperCase() + inputValue.slice(1);
        }
    document.getElementById('capitalizeInput')['value'] = inputValue;
    });

提交回复
热议问题