473,699 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Javascript object and dynamically created buttons

Hi,

Can anyone work out why the following doesn't work. For some reason,
when you dynamically add a button to the document from a Javascript
object it can't then reference the properties of the object like a
"normal" button. Any ideas?

Thanks in advance,

Mike.

=============== =============== =============== =============== =======
<html>
<head>
<script type="text/javascript">

var obj;

function TestObject () {
this.ID = "testObject 1";
this.Init = TestObject_Init ;
this.Test = TestObject_Test ;
}
function TestObject_Init () {
this.Button = document.create Element("INPUT" );
this.Button.typ e = "button";
this.Button.val ue = "Dynamicall y Added Button - this should display
\"testObject 1\" but doesn't!";
this.Button.onc lick = this.Test;
document.body.a ppendChild(this .Button);
}
function TestObject_Test () {
alert(this.ID);
}

function Go() {
obj = new TestObject();
obj.Init();
}

</script>
</head>
<body onload="Go()">
<input type="button" value="Static Button - This works as it should."
onclick="obj.Te st();" />
</body>
</html>
=============== =============== =============== =============== =======

Mar 15 '06 #1
2 2272
mi*******@hotma il.com wrote:
function TestObject_Test () {
alert(this.ID);
}


It does not work because "this" in that case is not obj (instance of
"TestObject "), but it is obj.Button.
If purpose of this script is showing popup message "testObject 1", you
have to correct script a little.

var obj;

function TestObject () {
this.ID = "testObject 1";
this.Init = TestObject_Init ;
this.Test = TestObject_Test ;
}
function TestObject_Init () {
this.Button = document.create Element("INPUT" );
this.Button.typ e = "button";
this.Button.val ue = "Dynamicall y Added Button - this should
display \"testObject 1\" but doesn't!";
this.Button.onc lick = this.Test;
this.Button.par ent = this; //first change
document.body.a ppendChild(this .Button);
}
function TestObject_Test () {
alert(this.pare nt.ID); //second change
}

function Go() {
obj = new TestObject();
obj.Init();
}

Mar 15 '06 #2
Ahh, that makes sense. Thanks.

Mar 15 '06 #3

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

Similar topics

6
3859
by: Jon | last post by:
Hello, I have a datagrid and the data in it is dynamically created at runtime... For iCounter = 0 To dataset.Tables(0).Columns.Count - 1 Dim objbc As New BoundColumn() With objbc .DataField = dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName .HeaderText = dsAllPastReplies.Tables(0).Columns(iCounter).ColumnName If .DataField = dsAllPastReplies.Tables(0).Columns("ReplyID").ColumnName Then
5
1929
by: Girish | last post by:
I have TWO submit buttons of type IMAGE on my asp form. This renders as <input type="image">. I need to be able to eble the ENTER button for both buttons. Yes, I know that for the input type image, the submit happens automatically to the server. Heres my problem: I have two event handlers for both these buttons as stated below: BUTTON 1:
0
1740
by: maxrawson | last post by:
First, let me start by saying my asp.net experience is still in it's infancy so please bare with me as I try to explain my situation. I have created a single page that with the use of many controls (i.e. roundedcorners component www.4guysfromrolla.com], buttons and panels) functions like a tab control. The buttons are programmatically designed to make the corresponding panel visible. Sample aspx.vb code:
2
3411
by: Caesar Augustus | last post by:
First, let me start by saying my asp.net experience is still in it's infancy so please bare with me as I try to explain my situation. I have created a single page that with the use of many controls (i.e. roundedcorners component www.4guysfromrolla.com], buttons and panels) functions like a tab control. The buttons are programmatically designed to make the corresponding panel visible. Sample aspx.vb code:
3
2341
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users page. So whenever the Admin person comes to know about the new category in the market he will be adding as different Sub-Categories for example ABAP, BDC etc..etc.. on every click event as Checkboxes. And these controls(checkboxes) should remain...
2
924
by: ManjuViswam | last post by:
In my form I've dynamically created radio buttons. I'm not able to do validation for these radio buttons, bcoz its finding the length as undefined. How to do client side validation for dynamically created radio buttons using javascript. Thanks
8
3106
by: Samik R. | last post by:
Hello, I am using the innerHTML property of a div placeholder to update the contents, and the HTML is provided from a perl script on the server side. The perl script gets called through AJAX when I press a button in the first page. The returned HTML in the div has another button, which, when pressed, should call the same perl script again. Think of the program as some sort of wizard. The problem is, this works perfectly as expected in FF...
1
2278
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function //////////////////////////////////////////////////////////////////////////////////// version 1 : using global variables ////////////////////////////////////////////////////////////////// var _textField, _divColorPicker; // global vars window.onload = function() { _textField = document.getElementById('htxaMessage'); // fill...
3
4218
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row value (text value) here is the code if possible please help me Thanks beforehand <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0
8697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8622
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8892
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7767
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4382
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.