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

  • Home
  • SEARCH
  • 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 3286896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:25:56+00:00 2026-05-17T20:25:56+00:00

this seems to be a simple question and I hope you can help me.

  • 0

this seems to be a simple question and I hope you can help me.
I’d like to add that my code works the way I’m describing it. I’m not content with the way I solved my problem concerning my problem in incrementing [i] outside the for-loop.

What I want is to get a sidebar menu shown when one hovers over an image.

My CSS looks like:


GM_addStyle
(
'.colormenu {       \
 display:none;      \
 margin-left: 10px;     \
 margin-top: -55px;     \
 }              \
 .colormenu a {         \
 display: block;                \
 width: 30px;                   \
 }                          \
 .colorlist {                   \
 list-style-type: none;         \
 }                          \
'
);

Now I’m defining two variables. One containing some colors, the other containing some fruits.

var color = ['red','yellow','green','blue'];
var fruit = ['strawberry','banana','apple','blueberry'];

I then define two arrays:

var hoverstring = new Array(color.length);
var idstring = new Array(color.length);

Now I want to see whether the value of an element of var color matches a title of the images on the webiste.
For every element being smaller than the length of var color I create two links in a list being wrapped by a div and insert it after a href wrapping the img-element.

for (var i=0;i<color.length;i++) {
 $("<div id='colormenu"+i+"' class='colormenu'><ul class='colorlist'><li><a href='path"+color[i]+"'>Call</a></li><li><a href='path"+color[i]+"'>Chat</a></li></ul><div>").insertAfter("a[href$='"+fruit[i]+"']"); 


 hoverstring[i]="img[title$='"+fruit[i]+"']:first";
 idstring[i]="#colormenu"+i;

}

The last step I take and this is where I got problems with is to create a function for every element. This is just an example of what it looks like:

    $(hoverstring[0]).hover(function(){
        $(idstring[0]).toggle();
    });
$(hoverstring[1]).hover(function(){ $(idstring[1]).toggle(); });
$(hoverstring[2]).hover(function(){ $(idstring[2]).toggle(); });
$(hoverstring[3]).hover(function(){ $(idstring[3]).toggle(); });
$(hoverstring[4]).hover(function(){ $(idstring[4]).toggle(); });
$(hoverstring[5]).hover(function(){ $(idstring[5]).toggle(); });

This method works, but is not very handy I’d say. Instead of creating a high amount of functions I’d like to increment i and not do it manually.

Like:


for (var i=0;i<color.length;i++) {
 $("<div id='colormenu"+i+"' class='colormenu'><ul class='colorlist'><li><a href='path"+color[i]+"'>Call</a></li><li><a href='path"+color[i]+"'>Chat</a></li></ul><div>").insertAfter("a[href$='"+fruit[i]+"']");




 hoverstring[i]="img[title$='"+fruit[i]+"']:first";
 idstring[i]="#colormenu"+i;


$(hoverstring[i]).hover(function(){
        $(idstring[i]).toggle();
    });
}

I tried it several times, but for some reason i isn’t incremented in the for-loop. Have you got an idea why?

I really hope you can help me. Thank you in advance.

  • 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-17T20:25:56+00:00Added an answer on May 17, 2026 at 8:25 pm

    You can do something quick like this:

    $.each(color, function(i) {
      $("<div id='colormenu"+i+"' class='colormenu'><ul class='colorlist'><li><a href='path"+this+"'>Call</a></li><li><a href='path"+this+"'>Chat</a></li></ul><div>").insertAfter("a[href$='"+fruit[i]+"']");
      $("img[title$='"+fruit[i]+"']:first").hover(function(){
        $("#colormenu"+i).toggle();
      });
    });
    

    The difference here is that i is scoped to this callback, not referencing the same variable that’s changing with the loop each time.

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

Sidebar

Related Questions

This seems like a simple question, but I can't find it with the Stack
This sure seems like a simple error to fix. However, I somehow can't figure
This seems to be an absurdly simple question but Google and Stack Overflow searches
Warning - I am very new to NHibernate. I know this question seems simple
This is probably a simple question but I can't seem to find the solution.
This seems like a very simple and a very common problem. The simplest example
This seems like a pretty softball question, but I always have a hard time
This is probably not a simple question so I am not looking for a
This must be a very simple question, but I don't seem to be able
Ok, this has got to be a super simple problem. I just can't seem

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.