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 6636981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:15:18+00:00 2026-05-25T23:15:18+00:00

relative(?) links: http://api.jquery.com/each/ http://api.jquery.com/jQuery.each/ hello i got this navigation menu <table> <tr> <td><div id=menuItem1

  • 0

relative(?) links:

http://api.jquery.com/each/

http://api.jquery.com/jQuery.each/

hello

i got this navigation menu

        <table>
        <tr>
        <td><div id="menuItem1" class="menuItem"><a href="http://www.w3schools.com">PORTFOLIO</a></div></td>
        <td><div id="menuItem2" class="menuItem">ABOUT ME</div></td>
        <td><div id="menuItem3" class="menuItem">CONTACT</div></td>
        </tr>
        <tr>
        <td><div id="selectA1" class="selectA current"></div></td>
        <td><div id="selectA2" class="selectA"></div></td>
        <td><div id="selectA3" class="selectA"></div></td>
        </tr>
        </table>

the selectA class is a rectangle that will select the menuItem when your mouse moves over it

the long code would be like

$("#menuItem1").mouseover(function () {
    $("#selectA1").stop().animate({opacity: 1},{ queue: false, duration: 200 });
});

$("#menuItem2").mouseover(function () {
    $("#selectA2").stop().animate({opacity: 1},{ queue: false, duration: 200 });
});

$("#menuItem3").mouseover(function () {
    $("#selectA3").stop().animate({opacity: 1},{ queue: false, duration: 200 });
});

$("#menuItem1").mouseout(function () {
    $("#selectA1").stop().animate({opacity: 0},{ queue: false, duration: 400 });
});

$("#menuItem2").mouseout(function () {
    $("#selectA2").stop().animate({opacity: 0},{ queue: false, duration: 400 });
});

$("#menuItem3").mouseout(function () {
    $("#selectA3").stop().animate({opacity: 0},{ queue: false, duration: 400 });
});

but i thought it could be shorter if i’d loop over those

so i tried to loop through those menuItems so that the rectangle will appear for all menu items

what i tried in javascript, all didnt work

var i=1;
for (i=1;i<=3;i++) {
$("#menuItem"+i).mouseover(function () {
    $("#selectA"+i).stop().animate({opacity: 1},{ queue: false, duration: 200 });
});
}

and

var i=1;
while (i<=3) {
$("#menuItem"+i).mouseover(function () {
    $("#selectA"+i).stop().animate({opacity: 1},{ queue: false, duration: 200 });
});

and

$(".selectA").each(function (i) {
$("#menuItem"+i).mouseover(function () {
    $("#selectA"+i).stop().animate({opacity: 1},{ queue: false, duration: 200 });
});
}


i++;
}

thank you for your help

  • 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-05-25T23:15:19+00:00Added an answer on May 25, 2026 at 11:15 pm

    First of all, you would be better off with hover rather than a mouseover/mouseout pair.

    Secondly, you don’t need to use each at all, there is a nice simple relationship between your .menuItem and .selectA elements: they have the same suffix number in their id attributes. So, you could do the whole thing with something simple like this:

    $('.menuItem').hover(
        function() {
            var n = this.id.replace(/[^\d]/g, '');
            $('#selectA' + n).stop().animate({ opacity: 1 },{ queue: false, duration: 200 });
        },
        function() {
            var n = this.id.replace(/[^\d]/g, '');
            $('#selectA' + n).stop().animate({ opacity: 0 },{ queue: false, duration: 200 });
        }
    );
    

    Demo: http://jsfiddle.net/ambiguous/eza8b/

    As far as why this:

    for(var i = 1; i <= 3; i++) {
        $("#menuItem" + i).mouseover(function () {
            $("#selectA" + i).stop().animate({opacity: 1}, {queue: false, duration: 200 });
        });
    }
    

    doesn’t work goes, you’re having a classic closure problem. The functions that you supply to .mouseover are closures over i so all of them end up using the last value that i had and that value is 4; that means that all of the inner selectors end up as $('selectA4') and that doesn’t refer to anything useful. If you really want to use a loop then you can force i to be evaluated when you want it to be with this:

    for(var i = 1; i <= 3; i++)
        (function(n) {
            $("#menuItem" + n).mouseover(function () {
                $("#selectA" + n).stop().animate({opacity: 1}, {queue: false, duration: 200 });
            });
        })(i);
    

    or this:

    function build_animator(i) {
        return function() {
            $('#selectA' + i).stop().animate({opacity: 1}, {queue: false, duration: 200 });
        };
    }
    for(var i = 1; i <= 3; i++)
        $("#menuItem" + i).mouseover(build_animator(i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are all familiar with relative paths: A relative path to ./images/hello.jpg from http://www.domain.com/hey
I got these relative links in my phpbb forum which I'd like to replace
I got this code which works 100% correctly using the Bing Maps Api V6.1
I was looking at the documentation page for jScroll plugin for jQuery ( http://demos.flesler.com/jquery/scrollTo
I am working on this site: http://www.problemio.com and I am supposed to make a
I'd like all of my links that are not xxx.domain.com and not /relative links
Without arguing the relative merits of storing a datetime this way... If you had
I have a footer file that includes links and images. This file is used
Scenario: I am using jQuery to lazy load some html and change the relative
Background: Suppose I have a website that is hosted on http://www.example.com and the site

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.