1

With this code:

<body>
    <div id="a1" style="width:200px; height:100px; background-color:black; margin:10px;"></div>
    <div id="a2" style="width:200px; height:100px; background-color:black; margin:10px;"></div>

    <script>
        i = 1;
        $("#a" + i).hover(function(){
            $("#a" + i).css("background-color", "grey");
        }, function(){
            $("#a" + i).css("background-color", "black");
        });

        i = 2;
        $("#a" + i).hover(function(){
            $("#a" + i).css("background-color", "grey");
        }, function(){
            $("#a" + i).css("background-color", "black");
        });
    </script>
</body>

I cannot receive the hover effect for each element, when I over mouse on the first, the second element changed. I think jquery not same as javascript about the sequence of commands, may be the hover for the first still not run yet. I can use queue function to order the command?

1
  • 1
    Please format your code correctly. It will make it much easier for you, and others, to understand. Commented Aug 5, 2014 at 6:55

2 Answers 2

2

The issue is because the handlers are assigned on load, and executed well after that point when the mouseenter event fires. Because of that, i will always be 2.

A better method is to attach the same handler to both elements, and use the this keyword in the handler to relate the logic to the element which raised the event. Try this:

<div id="a1" class="foo"></div>
<div id="a2" class="foo"></div>

<script type="text/javascript">
    $('.foo').hover(function() {
        $(this).css("background-color", "grey");
    },
    function() {
        $(this).css("background-color", "black");
    });
</script>

Example fiddle

Note that by using classes on your elements, you can place the styling information in to a CSS stylesheet for a better separation of concerns.

Sign up to request clarification or add additional context in comments.

Comments

0

At the time function will be executed the value of i will be 2, So using this instead of selectors will help you

i = 1;
$("#a" + i).hover(function () {
    $(this).css("background-color", "grey");
}, function () {
    $(this).css("background-color", "black");
});

i = 2;
$("#a" + i).hover(function () {
    $(this).css("background-color", "grey");
}, function () {
    $(this).css("background-color", "black");
});

And I think you are repeating code you can write like bellow.

for(var i=1;i<=2;i++){
    $("#a" + i).hover(function(){
            $(this).css("background-color", "grey");
        }, function(){
            $(this).css("background-color", "black");
        });
}

1 Comment

The functions close around i, which cause it's value to be 2 by the time they get invoked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.