...

View Full Version : OO and Events



gsoft
12-23-2005, 04:51 PM
I am currently creating a small user object that will control some events which will be passed and store some data such as point values. Currently I am having some difficulties with the event method handleMouseDown with any property defined in the object.

Here is a small example of what I have


<html>
<head>
<title>OO Test</title>
<style type="text/css">

body {
margin-top: 50px;
margin-left: 100px;
}

#block {
background: #000;
width: 400px;
height: 300px;
}

</style>

<script type="text/javascript">

function OOTest(domObj)
{

this.cropElement = domObj;

this.test = new Array(2);
this.test[0] = 1;
this.test[1] = 2;

this.handleMouseDown = function(e)
{
alert(this.test);
};

// testing in firefox at the moment so this works. you may need to change this to this.cropElement.onmousedown = this.handleMouseDown;
this.cropElement.addEventListener("mousedown", this.handleMouseDown, true);

}

window.onload = function()
{
new OOTest(document.getElementById("block"));
}

</script>

</head>
<body>

<div id="block"></div>

</body>
</html>

In this when clicking down in the div it just alerts the message undefined even though the this.test has been defined and initiated. Same applies with any other property defined.

What do I need to do to get it to work properly?

gsoft
12-24-2005, 01:44 AM
With a little help, from another person I got it working here is the updated code if anyone has a similar problem



<html>
<head>
<title>OO Test</title>
<style type="text/css">

body {
margin-top: 50px;
margin-left: 100px;
}

#block {
background: #000;
width: 400px;
height: 300px;
}

</style>

<script type="text/javascript">

function OOTest(domObj)
{

var me = this;
this.cropElement = domObj;

this.test = new Array(2);
this.test[0] = 1;
this.test[1] = 2;

this.handleMouseDown = function(e)
{
alert(me.test);
};

// testing in firefox at the moment so this works. you may need to change this to this.cropElement.onmousedown = this.handleMouseDown;
this.cropElement.addEventListener("mousedown", this.handleMouseDown, true);

}

window.onload = function()
{
new OOTest(document.getElementById("block"));
}

</script>

</head>
<body>

<div id="block"></div>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum