473,466 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

onclick problem

Hi All,

I wrote some code but it doesn't work.

------------------------------------------
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error
------------------------------------------

Browser says "MyClass" has no property. But if I replace
onclick="MyClass.btnClickHandler" with onclick="alert(0)", it works. How
to bind an event to MyClass.btnClickHander ?

BTW: If I bind this event using Prototype, it works.


--
Xu, Qian (stanleyxu)
Jun 27 '08 #1
8 1104
"Xu, Qian" wrote:
------------------------------------------
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error
------------------------------------------

Browser says "MyClass" has no property. But if I replace
onclick="MyClass.btnClickHandler" with onclick="alert(0)", it works. How
to bind an event to MyClass.btnClickHander ?
A few changes help:
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body onload = MyClass.init()>
----------^ so that the DOM is complete
</html>

// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler()"
------------------------------------------------------------------^ brackets
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHandler : function() {
----------------^ spelling "l" missing
alert('Done');
}
}

// MyClass.init();
^ now in onload

Tom

Jun 27 '08 #2
Tom de Neef wrote:
>
// MyClass.init();
^ now in onload

Tom
Thanks, but this does not help. I am more interested in Why?
--
Best regards
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com/
Jun 27 '08 #3
Tom de Neef wrote:
>
// MyClass.init();
^ now in onload

Tom
Thanks Tom, but this does not help. I am more interested in Why?

One thing I do not get, when button is clicked successfully,
MyClass.btnClickHandler would be called. I called "alert(this.tagName)",
it is "BUTTON" not "MyClass". The "this" pointer should not be MyClass?

--
Best regards
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com/
Jun 27 '08 #4
"Xu, Qian"wrote:
>>
// MyClass.init();
^ now in onload

Tom

Thanks Tom, but this does not help. I am more interested in Why?

One thing I do not get, when button is clicked successfully,
MyClass.btnClickHandler would be called. I called "alert(this.tagName)",
it is "BUTTON" not "MyClass". The "this" pointer should not be MyClass?
I suggested three changes. I tested the code with these changes. It works
fine.
Tom
Jun 27 '08 #5
Xu, Qian escribió:
<html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error
I get this error:

bd has no properties
bd.innerHTML = injectCode + bd.innerHTML;

You call init() from <headtag, with no timer or event handling. At
that point, it's likely that the isn't any <bodyyet.

You should launch init() when page has loaded or when DOM is ready:

window.onload=function(){
MyClass.init();
};

The above lines are only a quick example, it's not the recommended way
to do it because you'll overwrite any other onload actions you may have
defined. If you get the addEvent() function from:

http://dean.edwards.name/weblog/2005/10/add-event2/

.... you could change this into:

addEvent(window, "load", function(){
MyClass.init();
});

You can also Google for "onDomReady" or "domReady".
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 27 '08 #6
Tom de Neef wrote:
"Xu, Qian"wrote:
>>// MyClass.init();
^ now in onload

Tom
Thanks Tom, but this does not help. I am more interested in Why?

One thing I do not get, when button is clicked successfully,
MyClass.btnClickHandler would be called. I called "alert(this.tagName)",
it is "BUTTON" not "MyClass". The "this" pointer should not be MyClass?

I suggested three changes. I tested the code with these changes. It works
fine.
Tom

Thanks, it was my fault.
1. It works when I use attachEventListner (for Firefox)
2. I should write

this.btnClickHander = function() {
// accessing methods or properties of another class works now ;)
}

--
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com
Jun 27 '08 #7
Álvaro G. Vicario wrote:
Xu, Qian escribió:
><html>
<head>
<script src="my_broken_script.js"></script>
</head>
<body>
Hello World
</body>
</html>
------------------------------------------
// my_broken_script.js
var MyClass = {
init : function() {
var injectCode = '<button onclick="MyClass.btnClickHandler"
id="btn1">Button 1</button>';
var bd = document.body;
bd.innerHTML = injectCode + bd.innerHTML;
},
btnClickHander : function() {
alert('Done');
}
}

MyClass.init(); // <-- Error

I get this error:

bd has no properties
bd.innerHTML = injectCode + bd.innerHTML;

You call init() from <headtag, with no timer or event handling. At
that point, it's likely that the isn't any <bodyyet.

You should launch init() when page has loaded or when DOM is ready:

window.onload=function(){
MyClass.init();
};

The above lines are only a quick example, it's not the recommended way
to do it because you'll overwrite any other onload actions you may have
defined. If you get the addEvent() function from:

http://dean.edwards.name/weblog/2005/10/add-event2/

... you could change this into:

addEvent(window, "load", function(){
MyClass.init();
});

You can also Google for "onDomReady" or "domReady".

Thanks for the tip. Yes, I should wait until the DOM tree has been
loaded completely. Currently I use attachEventListner for Firefox and it
works.

--
Xu, Qian (stanleyxu)
http://stanleyxu2005.blogspot.com
Jun 27 '08 #8
this.btnClickHander = function() {

I think you still miss an "l": Handler instead of Hander
Tom
Jun 27 '08 #9

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

Similar topics

8
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function...
5
by: Mike | last post by:
In my previous post, I wrote: > ... > GOAL: (very simple) Provide a hyperlink which, when clicked, > calls a javascript function which opens a new URL. > ... > PROBLEM: The following code...
17
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to...
8
by: KS | last post by:
Just to show some code to show the consept. <img id="date" onclick="javascript:show_calendar();" src="/PlexSysWeb/images/show-calendar.gif" width=20 height=18 border=0> What i want the...
5
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
7
by: abs | last post by:
Hi everyone. Please, check my test code here: http://skocz.pl/jstest . The trouble is that no matter which span element I click, it alerts '3' and I'm wondering why not '1' for the first span,...
3
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick...
2
by: stevemtno | last post by:
I've got a problem with a web page I'm working on. I have 4 modules - one of them has 2 tabs, two of them have 4 tabs. When the user clicks on the tabs, the content below them changes. However, when...
2
by: DavidGeorge | last post by:
In an earlier thread I recounted a problem I was having, but it took a while to reduce the problem to it's basic components and the issue became somewhat confused in reaching that point. I hope you...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.