473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Javascript Object Attach event not working

I am new to JavaScript objects. The object creates a button with no
events attached. Later I want to attach an event to the button created
by this object. The event attaches a function in the object.

The code is listed below but the event is never attached.

The 'this' reference for the button seem to be correct and the
'self'
reference to the control(object) also seems to be correct.

What am I doing wrong?
<html>
<script>

// control
function oeventControl(){}

oeventControl.prototype.paint = function(canvas){
this.mainBody = document.createElement("div");

this.mainBody.appendChild(document.createElement(" input"));
this.button = this.mainBody.getElementsByTagName("input")[0];
this.button.setAttribute("TYPE","BUTTON");
this.button.setAttribute("VALUE","click me after attaching
event");
this.button.style.color="red";

canvas.innerHTML= this.mainBody.outerHTML;

}

oeventControl.prototype.newevent = function()
{
alert("new event attached");
}
oeventControl.prototype.aevent = function(sColor)
{ // this button check using alert below
alert(this.button.outerHTML);

var self = this;

// self.newevent point to correct function
// self.newevent();

//
// why the code below does no attach the event and
// the color of the button does not change
this.button.onclick=function(){self.newevent();}
this.button.style.color=sColor;
}

// end control
var oc;
function initPage(){
oc = new oeventControl();
oc.paint(document.all.controlPaintAear);

}
function aEvent(){
oc.aevent("green");

}
</script>
<body onload="initPage()">
<div id="controlPaintAear"></DIV>
<BR/>
<BR/>

<INPUT TYPE="BUTTON" VALUE="Attach Event to the Control Button"
onclick="aEvent();">
</body>

Oct 22 '05 #1
4 2874
VK

bh********@gmail.com wrote:
I am new to JavaScript objects. The object creates a button with no
events attached. Later I want to attach an event to the button created
by this object. The event attaches a function in the object.

The code is listed below but the event is never attached.

The 'this' reference for the button seem to be correct and the
'self'
reference to the control(object) also seems to be correct.

What am I doing wrong?
<html>
<script>

// control
function oeventControl(){}

oeventControl.prototype.paint = function(canvas){
this.mainBody = document.createElement("div");

this.mainBody.appendChild(document.createElement(" input"));
this.button = this.mainBody.getElementsByTagName("input")[0];
this.button.setAttribute("TYPE","BUTTON");
this.button.setAttribute("VALUE","click me after attaching
event");
this.button.style.color="red";

canvas.innerHTML= this.mainBody.outerHTML;

}

oeventControl.prototype.newevent = function()
{
alert("new event attached");
}
oeventControl.prototype.aevent = function(sColor)
{ // this button check using alert below
alert(this.button.outerHTML);

var self = this;

// self.newevent point to correct function
// self.newevent();

//
// why the code below does no attach the event and
// the color of the button does not change
this.button.onclick=function(){self.newevent();}
this.button.style.color=sColor;
}

// end control
var oc;
function initPage(){
oc = new oeventControl();
oc.paint(document.all.controlPaintAear);

}
function aEvent(){
oc.aevent("green");

}
</script>
<body onload="initPage()">
<div id="controlPaintAear"></DIV>
<BR/>
<BR/>

<INPUT TYPE="BUTTON" VALUE="Attach Event to the Control Button"
onclick="aEvent();">
</body>


The posted code gives you "Object expected" error on load.
Could you post a working example of your problem?

Oct 22 '05 #2
VK wrote:
The posted code gives you "Object expected" error on load.
Could you post a working example of your problem?


VK
the code is a working logic.
In the line "click me after attaching event" , the word event gets
wrapped to the next line and gives the error.

line
this.button.setAttribute("VALUE","click me after attaching event");

with no wrapping should not give any error.

Thanks for all your help.

Oct 23 '05 #3
On 22/10/2005 12:51, bh********@gmail.com wrote:

[snip]
What am I doing wrong?
You're using a rather bizarre mix of DOM methods and proprietary IE
features. IE's behaviour is rather odd, but it can be avoided very
simply by going about each step rather more consistently. If you have
any intention of supporting other browsers - arguably a necessity if
you're working on something that will be public - changes will be essential.

[snip]
this.mainBody.appendChild(document.createElement(" input"));
this.button = this.mainBody.getElementsByTagName("input")[0];
Why didn't you just save a reference to the INPUT element in the first
place?
this.button.setAttribute("TYPE","BUTTON");
this.button.setAttribute("VALUE","click me after attaching event");
Attributes should be set before adding a newly-created element to any
document fragment, especially the type attribute (which is write-once).
It's also best to avoid the *Attribute methods as IE doesn't always
handle them well.

[snip]
canvas.innerHTML= this.mainBody.outerHTML;
Why not use the appendChild method?

oeventControl.prototype.paint = function(canvas) {
var mainBody = this.mainBody
= document.createElement('div'),
button = this.button
= document.createElement('input');

button.type = 'button';
button.value = 'Test';
button.style.color = 'red';

mainBody.appendChild(button);
canvas.appendChild(mainBody);
};

[snip]
oc.paint(document.all.controlPaintAear);


Use the getElementById method to obtain element references:

oc.paint(document.getElementById('controlPaintAear '));

The rest will work as-is. However, there are still issues with this code.

There is no feature detection. This may lead to errors and
uncontrollable failure in some browsers. How to rectify that would
depend on what you're actually trying to do.

The event listener you add dynamically to the INPUT element will cause a
memory leak. See
<URL:http://www.jibbering.com/faq/faq_notes/closures.html#clMem>.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 24 '05 #4
Thanks Mike
I did the changes as recommended and the code works fine now.

Oct 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
1
by: Darin Browne | last post by:
Here is my code created when a page loads the first time: string jScript = @" <script language='javaScript'> var pop = false;
5
by: Digital | last post by:
Hi, I'm looking for links to the JS object model for the not-as-popular properties and methods. I've found things like hasOwnProperty, isEnumerable, ===, prototype, and others by searching for...
4
by: David Veeneman | last post by:
Do I need to unsubscribe from an object's event before I point the object variable to a new object? I have a method that subscribes an event handler to the ListChanged event of a list. The list...
31
by: ajos | last post by:
hi frnds, i have a form,which has 2 input text boxes, the values are entering the text boxes,when i leave the 2 text boxes blank and hit submit a java script gives the message that the 2 fields are...
3
by: =?Utf-8?B?QmFycnkgR2lsYmVydA==?= | last post by:
I have a class that raises events that downstream objects subscribe to. In one case, after destroying the object, the event seems to still get handled in a subscriber object. So I instantiate an...
22
by: canabatz | last post by:
Hi all! i got a countdown timer for auctions that runing. i got a DIV that refreshs every 4 seconds that DIV is refreshed by ajax ,the file that is refreshed is timer.php for example!! my...
23
Frinavale
by: Frinavale | last post by:
JavaScript is a very strange place for me... So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a...
3
Frinavale
by: Frinavale | last post by:
I've created a few ASP.NET Ajax Enable Server controls. There are 2 components to these controls: a server side Object that deals with the server side stuffs, and a client side Object that deals...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.