How to change Input box borders after filling the box using JavaScript ?

In this article, we will change the input border after filling the text on the input text field. The onchange event attribute works when the value of the element changes and select the new value from the list.

Approach: Whenever the input value is changed with some value by the user, the onchange event attribute is triggered. After the event is fired, the value is checked if it is not null. If the user value exists, then the bottom border of the input control is changed to dotted red color by using inline styling.

Syntax:

<element onchange = "script">

Example: In this example, we will change the border bottom style when the user type some text in the field.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align: center;">
    <h2>GeeksForGeeks</h2>
  
    <h2>
        How to Change Input 
        borders after filling?
    </h2>
  
    <form>
        <label> NAME</label>
        <input type="text" id="fname" 
                name="fname" value="">
        <input type="submit" value="submit">
    </form>
  
    <script>
        var gfg = document.getElementById("fname");
        gfg.onchange = function (e) {
            if (gfg.value != '') {
                e.target.style.borderBottom 
                        = "4px dotted red";
            }
        };
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before filling the text:
  • After filling the text:

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.