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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:38:58+00:00 2026-05-27T10:38:58+00:00

The following is a simple bit of code that can be used to create

  • 0

The following is a simple bit of code that can be used to create image rollovers (taken from a tutorial that I’m going through). The author says: under the attach event handlers part, the constructor creates the two event handlers here. It’s within these event handlers that the variable named “that” has to be used to refer to the Rollover object. That’s because the “this” keyword within the event handlers refers to the image object, not the Rollover object.

My question is: I understand that this.image refers to the image object but wouldn’t you want to just set this.image.src equal to this.newImageURL? I guess I just don’t understand why that is necessary. And how is it that changing the src property of that.image ends up changing the src property of `this.image?’

var $ = function (id) { return document.getElementById(id); }

var Rollover = function ( imageId, newImageURL ) {
    var that = this;
    this.newImageURL = newImageURL;
    this.image = $(imageId);

    // Validate node
    if ( ! this.image ) {
        throw new Error("Rollover: Image ID not found.");
    }
    if ( this.image.nodeType !== 1 || this.image.nodeName !== "IMG" ) {
        throw new Error("Rollover: Image ID is not an img tag.");
    }

    var newObj = that;
    // Copy original image url
    this.oldImageURL = this.image.src;

    // Attach event handlers
    this.image.onmouseover = function () {
        that.image.src = that.newImageURL;
    }
    this.image.onmouseout = function () {
        that.image.src = that.oldImageURL;
    }
}
  • 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-27T10:38:58+00:00Added an answer on May 27, 2026 at 10:38 am

    In javascript the this variable changes with scope. The first line of var that = this; this refers to the Rollover object. Setting that = this basically just creates a pointer to Rollover.

    In any subsequent event call like

    this.image.onmouseover = function () {
        that.image.src = that.newImageURL;
    }
    

    In the function callback this now refers to the event scope – this now points to the object that was rolled over. Thats why this.image wouldn’t point to anything because the DOM element being moused over does not have an image property. Because you already set another variable (that) to the Rollover instance (original this), you can now use it in any of the other scopes.

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

Sidebar

Related Questions

My problem is a bit more complex than using the following simple JavaScript code:
I have a very simple bit of code that won't work, and have no
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
The following simple calculator expression grammar (BNF) can be easily parsed with the a
We have the following simple Stored Procedure that runs as an overnight SQL server
Consider the following simple C program that read a file into a buffer and
I have the following simple button with an image as the template. The problem
I have the following code, which does work: var dataSource = (from p in
When doing metaprogramming using C++ templates, is there a method that can be used,
This is a rather simple question that is making me a bit curious. Consider

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.