Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:05:32+00:00 2026-06-09T00:05:32+00:00

I wrote this code but it doesn’t work: JavaScript: $(function() { var menu_h_number=5 for

  • 0

I wrote this code but it doesn’t work:

JavaScript:

$(function() { 
    var menu_h_number=5

    for (i=1; i<=menu_h_number; i++)
    {
        $(".web_header_mb_"+i).show(1000);

        $(".web_header_mb_"+i).css("background", "#FF0000");

        $(".web_header_mb_"+i).hover(function () 
        {
            $(".web_header_mb_"+i).css("width", "200");
        });

        $(".web_header_mb_"+i).mouseout(function () 
        {
            $(".web_header_mb_"+i).css("width", "300");
        });
    }
});

HTML:

<div id="menu" class="web_header_mb_1"></div>
<div id="menu" class="web_header_mb_2"></div>
<div id="menu" class="web_header_mb_3"></div>
<div id="menu" class="web_header_mb_4"></div>
<div id="menu" class="web_header_mb_5"></div>

When start show different ids in the bucle but when I do a mouseover, there’s no change to the size.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:05:34+00:00Added an answer on June 9, 2026 at 12:05 am

    Why it doesn’t work

    The reason your code doesn’t work is this:

    i will have the correct value for code that is executed immediately (e.g. the show and hover calls). But, because of the way JavaScript works, this doesn’t work for callback (such as the one you give to hover). JavaScript will remember the variable, not the value of the variable at the time the callback was provided. The callback won’t be called until after the loop is completed. That’s why in the callbacks i will always be 5, because that was i‘s last value.

    You can read more about that here: Closures (MDN)

    Also, be aware that id‘s must be unique. You can’t give the id “menu” to five different elements; that’s what classes are for. In other words: you’ve got id and class backwards in your code.

    How to make it work

    The easiest way to circumvent the closure “problem” is to use $(this) inside the callback functions. In jQuery, the this keyword inside a callback function always points to the object which triggered the event. By using $(this) you have exactly the right jQuery object, without any fuss:

    for (i=1; i<=menu_h_number; i++)
    {
        var currentItem = $(".web_header_mb_" + i);
    
        currentItem
            .show(1000)
            .css("background", "#FF0000");
            .hover(
                function() { // mouseenter
                    $(this).css("width", 200);    // <--
                },
                function() { // mouseleave
                    $(this).css("width", 300);    // <--
                });
    }
    

    Another thing I did in the code above is buffer the jQuery object in a local variable (currentItem). This makes your code faster, because you only have to look up the element once (instead of 6 times, in this case). You should do this as much as possible.

    Also, as you can see, the hover function isn’t just for the mouseover event. You can give it callbacks to handle both mouseover and mouseout.

    One other thing you could do, as others have already suggested, is use a single class instead of 5 different classes. The jQuery function ($()) will actually return a collection if the query matches more than one object.

    So, given the following HTML:

    <div class="menu web_header_mb"></div>
    <div class="menu web_header_mb"></div>
    <div class="menu web_header_mb"></div>
    <div class="menu web_header_mb"></div>
    <div class="menu web_header_mb"></div>
    

    You could use each(), like this:

    $(".menu.web_header_mb").each(function() {
        $(this)
            .show(1000)
            .css("background", "#FF0000");
            .hover(
                function() { // mouseenter
                    $(this).css("width", 200);
                },
                function() { // mouseleave
                    $(this).css("width", 300);
                });
    });
    

    Or even this:

    $(".menu.web_header_mb").
        .show(1000)
        .css("background", "#FF0000");
        .hover(
            function() { // mouseenter
                $(this).css("width", 200);
            },
            function() { // mouseleave
                $(this).css("width", 300);
            });
    

    That last one works because show(), css() and hover() all work on jQuery collections (as well as single jQuery objects). Neat, huh?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code to try threads on Android, but it doesn't work. @Override
My brother wrote this code for me to show an example of polymorhism. But
HI all, I wrote this code but it doesn't catch any of the two
I want to convert function object to function. I wrote this code, but it
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I have write this code but it does work only if I am already
Im new to ruby, wrote this code and works but i know is not
I wrote this code for zoom in/out . it works but even with one
I wrote this code. The constructor works normally, but in the destructor I get
I have to fix a glitch with this code someone wrote but I'm not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.