473,556 Members | 2,475 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 2267
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
3854
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 =...
5
1920
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...
0
1728
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...
2
3406
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...
3
2331
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...
2
920
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
3095
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...
1
2259
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...
3
4200
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"...
0
7828
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. ...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7589
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7907
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...
0
6178
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...
1
5452
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...
0
5171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3597
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...
0
3576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.