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

DOM question

I have the following line of code:

var newopt = document.createElement("option");

I addChild this to a select list node and it works fine.

I want to attach an object reference to newopt, but the system doesn't
allow me to do this:
newopt.createAttribute("graph")

nor does it allow this:
newopt.setAttribute("graph",new MyObject)

I can't find any mention in the DOM that attributes cannot be added to
element nodes.

My question, then, is: how can I attach an object reference to an option
node?

Jul 23 '05 #1
8 1848
Dont know for sure this is what you were looking for but..

This work in IE6:

<html>
<head>
<title></title>
<script type="text/javascript">
function init(){

var newOption = new Option('string', 'optionValue');
var elSelect = document.getElementById("selectID");
var elDiv = document.getElementById("divID");

elSelect.options[0] = newOption;
var attr = document.createAttribute('graph');

elSelect.options[0].setAttributeNode(attr);
elSelect.options[0].setAttribute('graph', elDiv);
alert(elSelect.options[0].getAttribute('graph').tagName);
}
</script>
</head>

<body onload="init()">
<div id="divID"></div>
<select id="selectID"></select>
</body>
</html>

Oeyvind
--
http://home.online.no/~oeyvtoft/ToftWeb/


Jul 23 '05 #2
On Fri, 10 Sep 2004 08:58:32 +0200, oeyvind toft <oe******@online.no>
wrote:
Dont know for sure this is what you were looking for but..

This work in IE6:


However, it won't work in any other browser. The setAttribute method takes
two arguments; both strings. All that will happen on compliant browsers is
the object will have its toString method called, and the result will be
assigned to the attribute.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
On Fri, 10 Sep 2004 01:24:58 GMT, Richard Trahan <rt*****@optonline.net>
wrote:

[snip]
newopt.setAttribute("graph",new MyObject)

I can't find any mention in the DOM that attributes cannot be added to
element nodes.

My question, then, is: how can I attach an object reference to an option
node?


As I said in response to Oeyvind's message, the setAttribute approach
cannot work as the arguments must be strings. However, you don't need to
worry about the *Attribute methods as you can create a new property just
as you would with any other object:

newopt.graph = new MyObject();

The *Attribute methods are designed to dynamically create attributes that
you would normally write directly with HTML. As you're not doing this,
it's inappropriate to use them. That said, HTML attributes (but not
XHTML/XML attributes) can be directly accessed using their property names,
like aObj.href and imgObj.alt.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Michael Winter wrote:
newopt.graph = new MyObject();


Thank you for responding, Mike. That does work; I seem to remember
trying that first and having trouble with it, but it works fine now.

However, this doesn't work:

var clone = child.cloneNode(true)

'child' is a select list option node, and contains my graph property
which I set with
child.graph = new MyObject()

'graph' disappears after the cloneNode(). I'm using Venkman on nn 7.1
and graph is clearly present in child and absent in clone. The DOM
reference on cloneNode indicates that everything should be copied,
including subtrees (which isn't relevant here). Could it be that the
browser interpets "clone" to refer only to values, not to attributes,
and will therefore only yield attributes defined in the DOM? If so, I
would have to maintain a parallel set of structures, which is a PITA.
I'm new at all this, so I may be missing something.

Jul 23 '05 #5
On Fri, 10 Sep 2004 16:51:57 GMT, Richard Trahan <rt*****@optonline.net>
wrote:

[snip]
var clone = child.cloneNode(true)

'child' is a select list option node, and contains my graph property
which I set with
child.graph = new MyObject()

'graph' disappears after the cloneNode(). I'm using Venkman on nn 7.1
and graph is clearly present in child and absent in clone. The DOM
reference on cloneNode indicates that everything should be copied,
including subtrees (which isn't relevant here). Could it be that the
browser interpets "clone" to refer only to values, not to attributes,
and will therefore only yield attributes defined in the DOM?
Clone copies all attributes, even custom ones, but you aren't adding an
attribute (you can't, as I explained). There is no requirement that
cloneNode copies user-defined properties.
If so, I would have to maintain a parallel set of structures, which is a
PITA.


Why? You could just write something like:

function cloneGraph() {
var t = this.cloneNode(true);
if(t) {t.graph = this.graph;}
return t;
}

function createOption() {
var e = document.createElement('OPTION');
if(e) {
e.graph = new MyObject();
e.clone = cloneGraph;
}
return e;
}

var child = createOption();
// do stuff
var clone = child.clone();

unless I'm missing something you're not telling us.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Michael Winter wrote:
(snip)

Thanks again. I was confused about the meaning of clone, due partly to
inconsistent wording in the DOM: "The duplicate node returned by
cloneNode() has no parent. Cloning a node copies all of its attributes
and their values..."

To me, "duplicate" means everything, including user properties, but
"attributes and their values" apparently implies "and nothing else".

Learning this stuff is a nightmare. Can you recommend a book or tutorial
on the DOM?

Jul 23 '05 #7
On Fri, 10 Sep 2004 19:38:35 GMT, Richard Trahan <rt*****@optonline.net>
wrote:
Michael Winter wrote:
(snip)

Thanks again. I was confused about the meaning of clone, due partly to
inconsistent wording in the DOM: "The duplicate node returned by
cloneNode() has no parent. Cloning a node copies all of its attributes
and their values..."

To me, "duplicate" means everything, including user properties, but
"attributes and their values" apparently implies "and nothing else".
I don't think it's inconsistent; the apparent deviation is a product of
the nature of Javascript. In other languages that provide bindings for the
DOM (Java and C++, for example), you aren't able to modify an object. They
must be defined in advance.

In Java, you'd create a class that implements the Element interface (or
perhaps inherits from a class that already does), add your graph member,
and modify the cloneNode method to copy the graph and call the base class'
cloneNode. This is much more explicit, though it is, in fact, exactly what
I suggested.
Learning this stuff is a nightmare. Can you recommend a book or tutorial
on the DOM?


Not personally. I learn through specifications and experimentation. :)

I can only suggest you check the resources in the FAQ
(<URL:http://jibbering.com/faq/>), though there don't seem to be many
appropriate links. Others reading this thread might be able to suggest
more.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
Richard Trahan wrote:
<snip>
Thanks again. I was confused about the meaning of clone,
due partly to inconsistent wording in the DOM: "The
duplicate node returned by cloneNode() has no parent.
Cloning a node copies all of its attributes and their
values..."

To me, "duplicate" means everything, including user
properties, but "attributes and their values" apparently
implies "and nothing else".

<snip>

You should bear in mind that the DOM specifications are intended to be
language neutral. Adding arbitrary properties to objects is a very
ECMAScritp thing to be doing (and as DOM objects would qualify as 'host
objects' there is actually no good reason to expect that to work, beyond
precedence), in a language such as Java adding a property to an object
in a DOM implementation is unthinkable. That means that the
specification cannot imply behaviour that cannot be implemented in any
language (only the bindings for specific languages could do that).

Richard.
Jul 23 '05 #9

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

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.