If input value is blank, assign a value of “empty” with Javascript

后端 未结 6 1386
庸人自扰
庸人自扰 2021-02-01 21:49

So I have an input field, if it\'s blank, I want its value to be the words \"empty\", but if there is any value inputted, I want the value to be the inputted value. I want to us

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 22:36

    I think the easiest way to solve this issue, if you only need it on 1 or 2 boxes is by using the HTML input's "onsubmit" function.

    Check this out and i will explain what it does:

    
    

    So we have created the HTML text box, assigned it a name ("val" in this case) and then we added the onsubmit code, this code checks to see if the current input box is empty and if it is, then, upon form submit it will fill it with the words empty.

    Please note, this code should also function perfectly when using the HTML "Placeholder" tag as the placeholder tag doesn't actually assign a value to the input box.

提交回复
热议问题