473,396 Members | 1,766 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,396 software developers and data experts.

passing object property to DOM PROPERTY (NOT ATTRIBUTE!)

This is my problem:

Expand|Select|Wrap|Line Numbers
  1.  
  2. $(document).ready(function(){
  3.  
  4. subClass.prototype = document.createElement( 'div' );
  5. subClass.prototype.constructor = subClass;
  6. function subClass() {
  7.     this.x = 5;
  8.     }
  9.  
  10. var divTested = new subClass;
  11. divTested.id = 'test';
  12.  
  13. alert(divTested.x); // works (result is 5)
  14.  
  15. document.body.appendChild(divTested);
  16.  
  17. $('#test').click(function() {alert(this.x);}); // doesnt work!
  18. $('#test').css({'height': 30, 'width': 50, 'background': 'red'});
  19.  
  20. });
  21.  
Jul 16 '11 #1
1 1041
Dormilich
8,658 Expert Mod 8TB
the problem why this.x doesn’t work is inheritance and the scope of your calls.

you have a <div> element, that is shared among all subClass instances (because you put it into the prototype).

when you create a subclass instance (divTested), you create a (primary) property x which is an unique (for that instance) instance property.

later you assign the id property, now is where the inheritance chain hooks in.
- first JS looks if there is (what I called primary property) an instance property named id – there isn’t
- next it looks in the prototype chain for such a property – it finds one in the inherited <div> => end
- if there would have been no such property in the prototype chain, a new (primary) instance property is created

up to line 14 no problems.

then it starts to go wrong. appendChild() expects a DOM Node to be given. divTested ain’t one, but it contains one in it’s inheritance chain so I assume, it uses the inherited <div> to insert into the document. that obviously drops everything related to the subClass instance.

next problem at line 17. you access the <div> element. as the <div> element is in the inhertance chain above the subClass instance, it has no knowledge of the x property of the subClass instance and thus you get undefined.
Jul 16 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Devante | last post by:
Hi, I am fairly new to ASP.NET and have been working on a web form that will allow a user to upload images to a database. I have found a sample web form that I have been trying to get working,...
1
by: windandwaves | last post by:
Hi Folk I am working with the TYPE property. I want to change that from 2 (byte) to 4 (long integer) for a field that already contains tons of data. How can I do that? The help reads: ...
2
by: Andrzej Kaczmarczyk | last post by:
Hi, I have following problem: (warning, quite long post with lots of code) I have an object of a known type like this. class MyClass { public int Property1; public string Property2; }
2
by: MattB | last post by:
I'm trying to implement an example I found for displaying images stored in a SQL database. The example code looks like this (in page_load): Dim connstr As String = "Integrated...
10
by: Laura K | last post by:
Tried all kinds of things. I get the error. The ConnectionString property has not been initialized. Any help appreciated. ...
0
by: martin_saucier | last post by:
I have a problem where when passing object ByRef in a WebService, some object members are being return as Nothing when coming back from the call ? How do I fix this ? Thanks. Martin
4
by: ianyian | last post by:
hi experts, im doing some staff bween th the aspx + MS Word.chellcheck, and and which running on my ypc is no problem ( windows xp , activation by ASPNET ), buts someshow when i try to deploy to...
4
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I had a class that worked, but I knew wasn't designed correctly from an OO point of view, so I changed it so it was more like I thought it 'should' be, and now it doesn't always work....
2
by: phyton2 | last post by:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using...
1
by: koyanpaing | last post by:
Hi everyone, I would like to know how can i know the value from the passing object.Here is the code example. <script> $trash.droppable({ accept: '#gallery > li', activeClass:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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...
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,...

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.