/    Sign up×
Community /Pin to ProfileBookmark

best method of getting an empty NodeList

I have noticed that

[code=php]var x = new NodeList();[/code]

doesn’t work, so for now I have been resorting to:

[code=php]var x = document.getElementsByTagName(‘no tag’);[/code]

which admitedly returns an empty NodeList, but I am convinced there has to be a more efficient method. Anyone any ideas?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 15.2008 — Why would you want an empty node list? The only reason I can think of is to detect of a node list needs to be filled, or if it's already filled.
// Example 1:
var NodeList = {length: 0};

if (NodeList.length < 1) {
NodeList = document.getElementsByTagName("[I]tag_name[/I]");
}

Or
// Example 2:
var NodeList = [];

if (NodeList.length < 1) {
NodeList = document.getElementsByTagName("[I]tag_name[/I]");
}


In example 1, NodeList is initially set to an object whose only property is [B]length[/B]. Since the test to fill the NodeList uses the .length property, you are kind of emulating the object structure of a node list.

In example 2 the NodeList variable is set to an empty array. When NodeList gets populated with data, it actually changes it's data type. It goes from being an array to an HTML node list. There are a few differences, for instance there is not [B]push[/B] method of a node list, but there is for an array.

Is this the reason for wanting to create an empty node list? If not, what is? I'm thinking some other programming issue is afoot if you want an empty node list.
Copy linkTweet thisAlerts:
@scragarauthorFeb 15.2008 — the reason I need a nodelist is because I am assigning functions to them using NodeList.prototype.[b]something[/b] = function(){[b]something[/b]};and I don't really want to be doing something like:
var x = document.getElementsByClassName('p');
if(typeof x != 'array')
x.something();

when I could just run:
document.getElementsByClassName('p').something();
Copy linkTweet thisAlerts:
@mrhooFeb 16.2008 — Any methods you assign to a nodeList prototype will either

throw an error (in IE), or set the methods on all Objects.

Read the constructor property of a nodelist- it will be undefined or Object.


document.getElementsByTagName("div").prototype

Returns undefined in all browsers

document.getElementsByTagName("div").constructor

Opera, Safari, firefox 2 return:

Object=function Object() {

[native code]

}

IE returns: undefined

document.getElementsByTagName("div").constructor.prototype

Opera, Safari, firefox 2 return: [object Object]

IE returns: undefined

You need your own object, that you can populate from a nodeList,

and then manipulate through the new object's prototype.
×

Success!

Help @scragar 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...