views:

71

answers:

1
<HTML>
<HEAD>
    <title>Login</title>
    <script type="text/javascript" src="http://script.auction.co.kr/common/jquery.js"&gt;&lt;/script&gt;     
    <script type="text/javascript">             
    $(document).ready(function() {
        $(":input[name='txtPassword']").blur(function(e){
            $(this).css("background-color","white");
        });
        $(":input[name='txtPassword']").focus(function(e){
            $(this).css("background-color","#FDFCDC");
        });
        $(":input[name='txtID']").blur(function(e){
            $(this).css("background-color","white");
        });
        $(":input[name='txtID']").focus(function(e){
            $(this).css("background-color","#FDFCDC");
        });


    });
    </script>
</HEAD>
<body>
    <form id="Form1" method="post" runat="server">
        <div id="body">     
            <input type="text" id="txtID" name="txtID" CssClass="txt" Width="130px" tabIndex="1"></input>
            <input type="password" id="txtPassword" name="txtPassword" Width="130px" TextMode="Password" tabIndex="2"></input>
        </div>
    </form>
</body>

Here is a code I wrote.

In this code, I feel very uncomfortable , that's because

two TextBoxes act almost same.

But I 'm not able to make this code shorter..

I think there must be optimal code.

I know below code work same. But I hope to attach the control name or ID. to read code easily

        $(":input").blur(function(e){
            $(this).css("background-color","white");
        });
        $(":input").focus(function(e){
            $(this).css("background-color","#FDFCDC");
        });
+7  A: 

Like this:

    $("#txtID, #txtPassword").focus(function(e){
        $(this).css("background-color","#FDFCDC");
    }).blur(function(e){
        $(this).css("background-color","white");
    });

Or, better yet:

<style type="text/css">
    #txtID, #txtPassword {
        background-color: white;
    }
    #txtID:active, #txtPassword:active {
        background-color: #FDFCDC;
    }
</style>
SLaks
which one is better? and y?
sunglim
The second one is better, because it doesn't need Javascript. Also, it will probably be faster.
SLaks
But is it supported in the same browsers as the first approach?
Horace Loeb
Correct me if I'm wrong, but I believe the second solution won't work on IE (at least some versions).
levik
@levik: I believe you're thinking about `:hover`.
SLaks
thank you for sharing your idea. the second one work correctly on every browser(FF,IE678,GC)
sunglim
:hover too. I thought pseudo class support only worked on links on some version of IE. Maybe I'm mistaken :)
levik