You must Sign In to post a response.
  • Category: JavaScript

    Change color of a textbox using javascript

    is it possible to change color of a textbox control by using javascript. if yes than how can we do that.
  • #747560
    Yes, javascript is enough sharp to handle these things, with the help of property 'style.backgroundColor' you can change the color of textbox
    see below snippet

    <script language="Javascript">
    function fillTextBox()
    {
    document.getElementById("subEmail").style.backgroundColor = "yellow";
    return false;
    }
    </script>

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #747573
    $(#txtName).css({background:'blue'});

  • #747577
    Hi Aman,

    Off course it's possible and its easy too. Just write a javascript function for this as below-

    function checkFill() {
    var inputElem = document.getElementById("txtbox");
    if (inputElem.value == "") {
    inputElem.style.backgroundColor = "yellow";
    }
    else
    {
    }
    }

    And add this to your text box-
    <asp:TextBox ID="txtbox" runat="server" onchange="checkFill();"></asp:TextBox>

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #748584
    see this demo.. :)

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script>
    function ChangeColor(id) {

    document.getElementById(id).style.backgroundColor = "Red";
    }

    </script>

    </head>
    <body>
    <input id="txtName" type="text" onblur="ChangeColor(this.id)"/>
    </body>
    </html>

    Regards,
    Nirav Prabtani (Senior Web Developer)
    Email : niravjprabtani@gmail.com
    blog : niravprabtani.blogspot.in


  • Sign In to post your comments