Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my textbox:
ASP
<asp:TextBox ID="TextBox2" runat="server" MaxLength="40" Width="325px" BackColor="LightSalmon" />


javascript:
XML
<script type="text/javascript" language="javascript">
    function checkFilled() {
        var inputVal = document.getElementById("dnn$ctr445$View$TextBox2").value;
        if (inputVal == "") {
            document.getElementById("dnn$ctr445$View$TextBox2").style.backgroundColor = "lightsalmon";
        }
        else {
            document.getElementById("dnn$ctr445$View$TextBox2").style.backgroundColor = "white";
        }
    }
</script>


pageload:
TextBox2.Attributes.Add("onchange", "javascript:checkFilled()")


my textbox is in an updatepanel. what I am trying to accomplish is the textbox back color is light red when empty. When a user types into it, the back color will turn white, and if they delete the textbox text it will turn back to light red. This is just for visual aid and not actually handling any validations.

I don't use a whole lot of javascript so I am not sure if my pageload part is correct.

can anyone give me a hand with why this code isn't working?
Posted

i hope you looking for this

http://jsfiddle.net/2Xgfr/14/[^]

JavaScript
<script type="text/javascript" language="javascript">
    function checkFilled() {
        var inputVal = document.getElementById('<%= TextBox2.ClientID %>').value;
        if (inputVal == "") {
            document.getElementById('<%= TextBox2.ClientID %>').style.backgroundColor = "lightsalmon";
        }
        else {
            document.getElementById('<%= TextBox2.ClientID %>').style.backgroundColor = "white";
        }
    }
</script>
 
Share this answer
 
v3
Comments
PTG.Jake 28-Nov-14 13:12pm    
great yes I used the example from jsfiddle, but I guess I was obtaining the ID incorrectly
[no name] 28-Nov-14 13:16pm    
+5 Sir. :)
King Fisher 28-Nov-14 23:21pm    
hi Suraj thank you :)
You could also use keyUp event, this event detects the value entered aswell as the backspace or delete clicked.
Check this fiddle
Fiddle[^]

I posted this answer just as I worked on this and found my option to be different. The answer Sir King has given is absolutely correct and great. I post this with no selfishness, this is just a share.
I hope you understand and look.
Thanks.
 
Share this answer
 
Comments
King Fisher 28-Nov-14 23:23pm    
good my 5+ :)
[no name] 29-Nov-14 0:15am    
Thank you Sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900