/    Sign up×
Community /Pin to ProfileBookmark

Simple Javascript Question about Node.data

I’ve decided to refresh (Update is a better word) my DOM scripting methods and I’m wondering the following:

document.getElementById([i]Elm[/i]).firstChild.data

^ Presuming the above is an element and the text is writable, id .data reac-only or is it writable.

Second, what is the difference between the above and

document.getElementById([i]Elm[/i]).firstChild.nodeValue

Cheers,

RyanJ

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@ExuroNov 20.2004 — The [FONT=courier new]data[/FONT] property is identical to the [FONT=courier new]nodeValue[/FONT] property for text nodes, but you cannot use the [FONT=courier new]data[/FONT] attribute to get the value of an attribute node.
Copy linkTweet thisAlerts:
@7studNov 20.2004 — I've decided to refresh (Update is a better word) my DOM scripting methods and I'm wondering the following:

document.getElementById(Elm).firstChild.data

^ Presuming the above is an element and the text is writable, id .data reac-only or is it writable.[/quote]


[color="red"]Warning!!![/color] Do not try to determine if the data property is writable yourself. Your computer will blow up causing caustic acid to scar you for life, and your dwelling will burn to the ground. [color="red"]Warning!![/color]

The data property is identical to the nodeValue property for text nodes[/quote]

That's what everyone seems to say, but I've never been able to find anything that makes clear what the differences between the nodeValue and data properties are. Do you have any sources?
Copy linkTweet thisAlerts:
@ExuroNov 20.2004 — [i]Originally posted by 7stud [/i]

[B]That's what everyone seems to say, but I've never been able to find anything that makes clear what the differences between the nodeValue and data properties are. Do you have any sources? [/B][/QUOTE]

[url=http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=28]http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=28[/url]

That's where I read about it...
Copy linkTweet thisAlerts:
@sciguyryanauthorNov 20.2004 — Thats a useful link, thanks!


RyanJ
Copy linkTweet thisAlerts:
@ExuroNov 20.2004 — Okay, I did a little research, and according to the [url=http://www.w3.org/TR/DOM-Level-2-Core/core.html]DOM Level 2 Specification[/url], the [FONT=courier new]nodeValue[/FONT] property is a member of the [url=http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247]Node interface[/url], meaning that any type of node which implements the Node interface should have the [FONT=courier new]nodeValue[/FONT] property. However, the [FONT=courier new]data[/FONT] property is specific to the [url=http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-FF21A306]CharacterData:Node interface[/url], and thus only CharacterData Nodes (text nodes) will have the [FONT=courier new]data[/FONT] property. I hope that helps to clear things up!
Copy linkTweet thisAlerts:
@sciguyryanauthorNov 21.2004 — Yup, that does.


Thanks for the help,

RyanJ
Copy linkTweet thisAlerts:
@7studNov 21.2004 — I've been to that howtocreate link before. Thanks.
I hope that helps to clear things up![/QUOTE]
Nope.?

However, the data property is specific to the CharacterData:Node interface,[/QUOTE]

Since the CharacterData type inherits from the Node type, the CharacterData 'interface'(which are its properties and methods) has the same 'interface' as the Node interface, as well as any additional interfaces(properties and methods) that CharcterData implements.

So, the question I have is: why is it necessary for CharacterData to implement the .data property when it already has the .nodeValue property it gets from the Node interface? In other words, what does the .data property allow a CharacterData interface to do that it couldn't already do with the .nodeValue property? ?
Copy linkTweet thisAlerts:
@ExuroNov 21.2004 — Here's my best guess. You're right about the CharacterNode inheriting the Node interface, so it would have the [FONT=courier new]nodeValue[/FONT] attribute, but I think the [FONT=courier new]nodeValue[/FONT] property is essentially a pointer which the sub-interfaces change to point at one of their own values. For example, the [url=http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-637646024]Attr Interface[/url] maps the [FONT=courier new]nodeValue[/FONT] property to its own [FONT=courier new]value[/FONT] property. My understanding would be that, as I said, [FONT=courier new]nodeValue[/FONT] is only a pointer and is used for the convenience of having a uniform property to access a node's data, but it cannot actually store any data, creating the necessity for the other properties such as [FONT=courier new]data[/FONT] and [FONT=courier new]value[/FONT]. I hope that makes some sense!
Copy linkTweet thisAlerts:
@7studNov 21.2004 — Yes, I see now that the Attr interface does the same thing with the its 'value' property as the CharacterData interface does with its 'data' property, namely they both seem to duplicate their inherited nodeValue property. I also see that the value/nodeValue pair for the Attr interface and the data/nodeValue pair for the CharacterData interface both return type DOMString, which means those properties are not implemented for a type switching reason.

It seems to me it would be atypical if nodeValue were a pointer since it is not only a read property, but a write property as well. For instance, javascript does not allow you to do this:

document.getElementById('my_div').firstChild="some text";

because document.getElementById('my_div').firstChild returns a pointer to an object, and it doesn't make sense to assign a string to a pointer. Instead you have to do this:


document.getElementById('my_div').firstChild.nodeValue = "some text"

which looks to me like you are using the pointer to get at the nodeValue property of the object, where the nodeValue property is a 'string' type, which then makes it ok to assign the string to nodeValue.

In addition, my js book doesn't even mention the 'data' property: it uses the 'nodeValue' property as the means for setting and getting a text node's string.
×

Success!

Help @sciguyryan 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,
)...