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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:59:26+00:00 2026-05-28T02:59:26+00:00

i’am a learner so i need still experience and i have many questions, but

  • 0

i’am a learner so i need still experience and i have many questions, but today i have only one question about the hover function.

is it possible to send the information about a variable from the first hover function into the seconde one? i think you dont need more information. is just changing a part from the src in a tag.

i hope you understand me. =)

here is the example

http://jsbin.com/eporib/edit#javascript,html,live

how is look now!

box.hover(function(){
    var $this = $(this),
                oldImg = $this.find('img').attr('src'),
                slicedOldImg = oldImg.slice(0,-5),
                newImg = slicedOldImg+num6;
    $this.find('img').attr('src', newImg);
      $('#hello').append('IN '+newImg);

}, function() {
              var $this = $(this),
                  oldImg = $this.find('img').attr('src'),
                  slicedOldImg = oldImg.slice(0,-5),
                  newImg = slicedOldImg+num1;
    $this.find('img').attr('src', newImg);
            $('#hello').append('OUT '+newImg+ '<br />');

}); /*end Hover*/

and how i want it

box.hover(function(){
    var $this = $(this),
                oldImg = $this.find('img').attr('src'),
                slicedOldImg = oldImg.slice(0,-5),
                newImg = slicedOldImg+num6;
    $this.find('img').attr('src', newImg);
      $('#hello').append('IN '+newImg);

}, function() {
    $this.find('img').attr('src', oldImg);
          //look here, the oldImg is in the first function.
          // and i want it use it in the second too.

            $('#hello').append('OUT '+newImg+ '<br />');

}); /*end Hover*/
  • 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-28T02:59:27+00:00Added an answer on May 28, 2026 at 2:59 am

    Yes, there are at least two ways you can do this:

    1. Using a closure

    Wrap the whole thing in a closure and use a variable in that closure:

    (function() {
        var oldImg;
    
        box.hover(function(){
            var $this = $(this), slicedOldImg newImg;
            oldImg = $this.find('img').attr('src'); // Setting it
            slicedOldImg = oldImg.slice(0,-5);
            newImg = slicedOldImg+num6;
            $this.find('img').attr('src', newImg);
              $('#hello').append('IN '+newImg);
    
        }, function() {
            // Missing `var $this = $(this);` here?
            if (oldImg) {
                // Use it
                $this.find('img').attr('src', oldImg);
    
                // and clear it
                oldImg = undefined;
            }
    
            $('#hello').append('OUT '+newImg+ '<br />');
    
        }    ); /*end Hover*/
    })();
    

    In that example, we use an anonymous wrapper function that we execute immediately for the closure, but you may well already be doing this code in a closure (a function), so you may not need to add that wrapper. (If the word “closure” is new to you, don’t worry, closures are not complicated.)

    2. Using data

    Alternately, you can use data to store the information on the element:

    box.hover(function(){
        var $this = $(this),
                    oldImg = $this.find('img').attr('src'),
                    slicedOldImg = oldImg.slice(0,-5),
                    newImg = slicedOldImg+num6;
        $this.data("oldImg", oldImg);
        $this.find('img').attr('src', newImg);
          $('#hello').append('IN '+newImg);
    
    }, function() {
        // Missing `var $this = $(this);` here?
        var oldImg = $this.data("oldImg");
        if (oldImg) {
            $this.find('img').attr('src', oldImg);
            $('#hello').append('OUT '+newImg+ '<br />');
        }    
    }    ); /*end Hover*/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.