Change css background style onclick textbox


I would like to have a jquery script that changes the background in my textboxes on my form when I click them and goes back to the previous style when I click on another element of the page.

Can someone show me how to achieve this?

Thanks for help!

Peter NordbergIT ManagerAsked:
Who is Participating?
Roopesh ReddyConnect With a Mentor IT AnalystCommented:

Check the below code!

  <asp:TextBox runat="server" />  

    <script type="text/javascript">
        $(document).ready(function () {
            $('input[type=text]').focus(function () {
                $(this).css("background", "#0f0");
            $('input[type=text]').blur(function () {
                $(this).css("background", "#fff");

Open in new window

Hope it helps u...
Chris StanyonCommented:
Unless you have a specific need to do this with jQuery, you can do it using just CSS.

input[type=text] { background-color:green; }
input[type=text]:focus { background-color:red; }

Open in new window

Peter NordbergIT ManagerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.