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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:18:08+00:00 2026-06-02T04:18:08+00:00

I am testing out the Raphael javascript library and would i’m experiencing a small

  • 0

I am testing out the Raphael javascript library and would i’m experiencing a small problem. I am drawing four shapes onto a canvas. I am then allowing an object to be the currently selected object via clicking. I then add an event handler listening for the press of the delete key. This works fine but i dont seem to be able to call a function. Below is the code to illustrate my problem.

This is a class that represents each of the cirlces on the canvas:

var Shape = new Class({
    initialize: function(x, y, color)
    {
        this.shape = paper.circle(x, y, 25);
        this.shape.attr("fill", color);

        this.shape.click(function()
        {
            if(selectedObj == null)
            {
                selectedObj = this;
                selectedObj.attr({'stroke':'orange', 'stroke-width':'5'});
            }
            else if(selectedObj != this)
            {
                selectedObj.attr({'stroke':'orange', 'stroke-width':'0'});
                selectedObj = this;
                selectedObj.attr({'stroke':'orange', 'stroke-width':'5'});
            }
            else if(selectedObj == this)
            {
                selectedObj.attr({'stroke':'orange', 'stroke-width':'0'});
                selectedObj = null;
            }
        });
    },
    deleteThis: function()
    {
        alert("Inside The deleteThis() Function.");
    }
});

This creates a shape and the when clicked it does the following:

  1. If the selectedObj is null, the shape that was clicked is set to the selectedObj and draws a small orangle colored rim around the shape.
  2. If the selectedObj is not the clicked shape, it takes the small border of the previously selected object and then sets the newly clicked shape as the currently selected item.
  3. If the shape is the selected object, when clicked again, it deselects it.

I then push four shapes onto the canvas:

objShapes.push(new Shape(50, 50, "red"));   
objShapes.push(new Shape(250, 50, "blue")); 
objShapes.push(new Shape(50, 250, "yellow"));   
objShapes.push(new Shape(250, 250, "green"));

Now that i can select and deselect an object on the canvas successfully, i need to be able to call functions of the selected object. To trigger the function i am using the keyboard. The following code is what i use:

$(document).keydown(function(event)
{
    if(event.keyCode == 46)
    {
        if(selectedObj == null)
        {
            alert("nothing to delete");
        }
        else
        {
            alert("Deleting " + selectedObj.attr('fill') + " Circle.");
            selectedObj.deleteThis();
        }
    }
});

Only part of the event handler work. When i hit the delete key and nothing is selected, an alert windows tells me nothing is selected to delete, However, when i have selected an object and hit the delete key, an alert does come up and tells me what circle i am deleting but it then fails to call the deleteThis() function. I dont understand why it will call the .attr() function but not the deleteThis() function.

What is the reason for not being able to call the function?

  • 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-06-02T04:18:11+00:00Added an answer on June 2, 2026 at 4:18 am

    The problem is selectedObj is referencing the internal shape variable, not the object of your class. You can make it reference the object itself (see the code below; basically cache the object in a variable, typically called self or that, and then use it inside the closure as the object you are storing). Then to reference the shape do selectedObj.shape instead of just selectedObj. The confusion is caused by the fact that your class is named Shape and you have a member variable named shape. Perhaps consider changing one or the other of these names to avoid that confusion.

    Also, you should be using a debugger with a javascript console. If you were, you’d see an error message, something like “deleteThis is not defined on object”, that would have helped you track this down.

    Anyway, here’s the code with the above change:

    var Shape = new Class({
        initialize: function(x, y, color)
        {
            // To keep track of the object itself
            var self = this;
    
            this.shape = paper.circle(x, y, 25);
            this.shape.attr("fill", color);
    
            this.shape.click(function()
            {
                if(selectedObj == null)
                {
                    selectedObj = self; // Assign the object, not the shape
                    selectedObj.shape.attr({'stroke':'orange', 'stroke-width':'5'});
                }
                else if(selectedObj != self) // Compare the object, not the shape
                {
                    selectedObj.shape.attr({'stroke':'orange', 'stroke-width':'0'});
                    selectedObj = this; // Assign the object, not the shape
                    selectedObj.shape.attr({'stroke':'orange', 'stroke-width':'5'});
                }
                else if(selectedObj == self) // Compare the object, not the shape
                {
                    selectedObj.shape.attr({'stroke':'orange', 'stroke-width':'0'});
                    selectedObj = null;
                }
            });
        },
        deleteThis: function()
        {
            alert("Inside The deleteThis() Function.");
        }
    });
    

    And then make the same adjustment in the other part:

    $(document).keydown(function(event)
    {
        if(event.keyCode == 46)
        {
            if(selectedObj == null)
            {
                alert("nothing to delete");
            }
            else
            {
                alert("Deleting " + selectedObj.shape.attr('fill') + " Circle."); // Reference the shape here
                selectedObj.deleteThis(); // This is the object now
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am testing out mod rewrite for my site. I thought i would try
I'm testing out the MapKit framework on the iPhone and would really much like
I was testing out covariant return types and came across this problem. class Vehicle
I am testing out the very awesome Knockoutjs. I am facing a peculiar problem.
I'm just testing out replacing a whole page with another page using JavaScript and
I'm testing out the Google Places autocomplete feature here: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete I want to get
I am testing out the simple pong game found here: https://github.com/shangaslammi/frp-pong The problem is
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
In testing out our API, one of our testers found out that when they
i'm just testing out the csv component in python, and i am having some

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.