/    Sign up×
Community /Pin to ProfileBookmark

Help required – Create nodelist collection.

Hi,

Sorry if I sound like a nob.

I’m trying to create the following JavaScript object nodelist collection structure.

[CODE]myNode.item(0).name=’test1′;
myNode.item(1).name=’test2′;[/CODE]

There are many examples and ways to read the DOM structure, but I can’t find any reference on how to create your own.

If someone has a pointer to documentation or code example that’d be fantastic.

I’m reading the rhinoceros book and again can’t find an example of this construct, although it is noted in passing throughout the book.

Thanks in advance for any help, sorry I sound so stupid.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 06.2010 — I think you confound the terms: to [I]create a node[/I] vs. to [I]circle through the child nodes of a parent node[/I].

To create:

DOM nodes are of different types (elements, textNodes, attributes, comments, CDATA sections). You can not create a generic node, you can create a specified type of node, and any type of node can be created using a specific method. See also:

http://www.w3schools.com/Dom/dom_nodes_create.asp

To circle:

Use a loop, and the childNodes.length as limit
Copy linkTweet thisAlerts:
@Dr_GoodvibesauthorOct 06.2010 — Thank you for your reply,

What I'm doing is, returning an xhr.responseXML from server side XSLT as a fragment. This is then parsed. Inside the returned fragment is non-HTML code which can be parsed in the same way as any other DOM node/element, but it's used to construct the buttons at the bottom of the pop-up window.

ie.
[CODE]<badrabbit button="button">
<popbutton name="submit" value="Submit" onclick="etc">
<popbutton name="cancel" value="Cancel" onclick="etc">
</badrabbit>[/CODE]


This is done so that the server side XSL code and the client side Javascript don't become to inter-dependent, client side presentation only, server side code structure.

This all works fine and now all I'm doing is placing the button structure into an object to then display at the bottom of the pop-up window appended to the document.

An example of my old approach but same structure can be seen at concreteresurfacing dot co dot nz and then click on the pictures at the right or the about at the bottom of the page, you will create an XSLT pop-up window.

The following test snippet of code may help you see where I going wrong and maybe my un-educated approach.

You're right, I'm confused, I've done tons of work with XSL and used to the DOM building approach.

For some reason I was looking at attempting to create a JavaScript object.

Hadn't thought about just using the DOM construct.

So would it be best to just start from document.createElement( myButton ) and just build a DOM 'fragment'?

The Javascript (sans DOM) approach would not be an array, but a set of methods that just make it sort of look like one.


[CODE]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8" />
<title>Test Object Class</title>

<script type="text/javascript">

var BADRABBIT = function() {
return {

testLoop: function( obj ){

for ( i = 0; i < 2; i++ ) {
obj.oButt.item(i).name = 'Test' + i;
}
return obj;
},

testInit: function(){

this.oButt = new BADRABBIT.button();
/*Initilize to verify that name can be overwritten with a new value later on*/
this.oButt.item(0).name = 'test9';
return this;

},

button: function(){
this.requested = false;
this.length = 0;
/* This where I'm stuck */
/* ???????????????????? */
this.item = function(){}
/*????????????????????? */
},

testStart: function(){
var obj = BADRABBIT.testInit();
obj = BADRABBIT.testLoop( obj );
alert( 'Name: ' + obj.oButt.item(1).name );
return obj.oButt.item(1).name
}
}

}()

</script>

</head>
<body>
<script type="text/javascript">
document.write( 'Name: ' + BADRABBIT.testStart() );
</script>
</body>
</html>[/CODE]


Thanks again for taking pity on this fool. ?
×

Success!

Help @Dr_Goodvibes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...