My simple input in HTML/CSS works perfectly, but when I tried to automate the process by writing a JavaScript function to create multiple inputs, I encountered an issue. The input created with JavaScript is already considered valid (from a CSS ":valid" standpoint) while the one created in just HTML/CSS is not. I need the input to start off as invalid when it has no content, and then become valid once something is entered. You can view the code on this CodePen. In summary, I want to know how to make the JavaScript version behave the same way as the HTML/CSS version.
I have attempted using both .setCustomValidity(0)
and .value = ''
, but encountered issues with both. The first solution causes the input to remain :invalid even after text is typed, while the second solution does not work at all.