Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to change Input box borders after filling the box using JavaScript...

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

In this article, we will change the input border after filling in the text on the input text field. The onchange event attribute works when the value of the element changes and selects 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 types some text in the field.

HTML




<body style="text-align: center;">
    <h2>GeeksForGeeks</h2>
  
    <h2>
        Change input
        borders after filling data
    </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>


Output:

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

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

Supported Browsers: 

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments