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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:00:57+00:00 2026-05-13T14:00:57+00:00

Pretty new to jquery and having a bit of a problem with a function

  • 0

Pretty new to jquery and having a bit of a problem with a function I wrote, very similar to this problem, and if that’s anything to go on then apparently I have a closure problem.
As Peter Bailey put it in the above thread, this is what’s happening:

  1. Iterate over some values
  2. Define/assign a function in that iteration that uses iterated variables
  3. You learn that every function uses only values from the last iteration.
  4. WTF?

This is my code:

var pages=['home', 'about', 'events', 'info', 'qa', 'photos', 'contact'];

for (i in pages) {

    $link='"'+"[href$='gaction.php?page="+(pages[i])+"']"+'"';
    $source="/images/"+(pages[i])+".png";

    $($link).hoverIntent(function() {
        $('#logo_text').stop(true, true).fadeOut(
        0,
        function() {
            $('#logo_text').attr('src', $source).fadeIn('slow'); // swaps logo
        });
    }, function() {
        $('#logo_text').stop(true, true).pause(300).fadeOut(
        0,
        function () {
            $('#logo_text').attr('src', '/images/name.png').fadeIn('slow'); //swaps back
        });
    });
}

I know the $link definition is pretty messy, but that bit works.

The function’s meant to replace the picture in #logo_text with one depending on the link that’s hovered over, but by the end of it every link changes the picture to the ‘contact’ (last) one.

Didn’t really understand how to fix it from the other thread so if someone can help out that would be great!

  • 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-13T14:00:57+00:00Added an answer on May 13, 2026 at 2:00 pm

    You can just use the jQuery.each statement, and by its very structure creates a closure. Then, be sure to declare the variables with var and you will be all set:

    var pages=['home', 'about', 'events', 'info', 'qa', 'photos', 'contact'];
    
    $.each( pages, function(){
      var $link='"'+"[href$='gaction.php?page="+ this +"']"+'"';
      var $source="/images/"+ this +".png";
    
      ... rest of function untouched
    
    });
    

    There are a few ways to rewrite the function, but in all honesty, if yours works for you (the selectors) then you are good. Only, instead of using fadeOut(0, function ...) just use hide().attr('src', $source ).fadeIn('slow')

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

Sidebar

Related Questions

Im pretty new to Dreamweaver. Im having a problem. I have a website that
I'm pretty new with JQuery and I'm having trouble creating a behaviour that would
I'm pretty new to jQuery (and javascript for that matter), so this is probably
I am still pretty new to using jQuery to do anything, but I have
im pretty new to jQuery, and i dont know how to do that, and
I'm pretty new to JQuery and JQuery-UI, but I'm having fun with it so
I'm pretty new to jQuery, so this is basically a syntactical question. I'm using
So I am pretty new to JQuery and just spent 5 hours getting this
I am still pretty new to jQuery, so forgive me if this doesn't make
I'm pretty new to require.js and having problems loading i18next.js. main.js require([lib/jquery, lib/i18next, config.i18next,

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.