473,408 Members | 2,888 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,408 software developers and data experts.

setAttribute versus assigning a property

Suppose I have a DOM element, say a td, and I want to add a
value to it to be used later. I am unclear on when it's OK to do

td.myAttr = "hello";

versus when I need to do

td.setAttribute("myAttr", "hello");

Could somebody explain the difference between these two?
In an application I'm working on, a lot of the code uses the
first method, and it seems to work most of the time, but
sometimes not, and I'm trying to understand the pattern.
Thanks.

Apr 26 '06 #1
4 3542
mitch said the following on 4/26/2006 4:08 PM:
Suppose I have a DOM element, say a td, and I want to add a
value to it to be used later. I am unclear on when it's OK to do

td.myAttr = "hello";

versus when I need to do

td.setAttribute("myAttr", "hello");

Could somebody explain the difference between these two?
One is buggy in IE and the other isn't.
In an application I'm working on, a lot of the code uses the
first method, and it seems to work most of the time, but
sometimes not, and I'm trying to understand the pattern.


When it fails would be a better indication of why it failed than to say
"It fails". Find an example of where domElementRef.property doesn't work
when domElementRef.setAttribute(property,propertyValue) does and post it
along with relevant HTML.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 27 '06 #2
VK

mitch wrote:
Suppose I have a DOM element, say a td, and I want to add a
value to it to be used later. I am unclear on when it's OK to do

td.myAttr = "hello";

versus when I need to do

td.setAttribute("myAttr", "hello");

Could somebody explain the difference between these two?


In the first case you are dealing with the exposed scriptable DOM
interface, in the second case you are working directly with the DOM
tree. The difference may seem subtle, but it is revealed on
standard-compliant browsers like Firefox. Say if you define a div like
<div id="myDIV" foobar="something">Content</div>
then both attributes will be parsed and added to the tree. But on
var ref = document.getElementById('myDIV');
you are not getting the relevant tree fragment. Instead you are getting
DOM interface, and it will contain only properties and methods listed
for this interface. This way ref.id will give you "myDIV" but
ref.foobar will give you undefined. At the same time get/setAttribute
works directly with the tree. It is slower than DOM interface, but you
can get any parsed nodes this way, listed and not listed in the
interface.
ref.getAttribute('foobar') // "something"

Apr 27 '06 #3
mitch wrote:
Suppose I have a DOM element, say a td, and I want to add a
value to it to be used later. I am unclear on when it's OK to do

td.myAttr = "hello";

versus when I need to do

td.setAttribute("myAttr", "hello");
If taken literally: neither one. You are dealing with host objects.
Do not expect that it is possible to add arbitrary properties to them
or to the items they represent. See ECMAScript 3,
Could somebody explain the difference between these two?
The first assignment creates a new property of the DOM host object
referred to, if that host object does not have it already and allows
that, and assigns it the value of "hello".

The second statement adds a new attribute to the (element) node this
host object represents, if the element does not have it already, and
if the DOM allows that. Although recommended by the W3C DOM Core
Specification, this approach usually is more error-prone than the
former, as setAttribute() implementations are known to be buggy
(i.e. do not follow the W3C DOM Core standard).
In an application I'm working on, a lot of the code uses the
first method, and it seems to work most of the time, but
sometimes not, and I'm trying to understand the pattern.


See? Avoid using host objects as containers for arbitrary information in
the first place. If you determined that the host object already has the
property, it is OK to assign it a value in the expected type/format, in
order to overwrite the current value. But do not try to create new
properties/attributes on it; "works for me" is not a reasonable design
approach.
PointedEars
Apr 28 '06 #4
VK

VK wrote:
mitch wrote:
Suppose I have a DOM element, say a td, and I want to add a
value to it to be used later. I am unclear on when it's OK to do

td.myAttr = "hello";

versus when I need to do

td.setAttribute("myAttr", "hello");

Could somebody explain the difference between these two?


In the first case you are dealing with the exposed scriptable DOM
interface, in the second case you are working directly with the DOM
tree. The difference may seem subtle, but it is revealed on
standard-compliant browsers like Firefox. Say if you define a div like
<div id="myDIV" foobar="something">Content</div>
then both attributes will be parsed and added to the tree. But on
var ref = document.getElementById('myDIV');
you are not getting the relevant tree fragment. Instead you are getting
DOM interface, and it will contain only properties and methods listed
for this interface. This way ref.id will give you "myDIV" but
ref.foobar will give you undefined. At the same time get/setAttribute
works directly with the tree. It is slower than DOM interface, but you
can get any parsed nodes this way, listed and not listed in the
interface.
ref.getAttribute('foobar') // "something"


Another key difference I missed to mention:

setAttribute("attrName", "attrValue") effectively equals to "runtime
typing in" <tagname attName="attValue">

It means that this way you can set only string values. if attValue is
not a string, a relevant toString() method will be called first.
Respectively getAttribute returns strings only. This way you cannot
store say object references via setAttribute, they will be stringified
and lost for you. You have to use DOM interface instead for that.

Apr 29 '06 #5

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

Similar topics

3
by: timmy_dale12 | last post by:
Hello , im a java programmer whos gotten tangled up in some javascripts. Im really stuck on this one can , can aybody explain this to me. I have a javscript which is to clone a table row and...
7
by: Bob Phillips | last post by:
Hope someone can help. I am struggling to write an meta element in JS. The meta element in question is the refresh element. This is the code I am using ...
3
by: duff | last post by:
Hiya, I'm trying to replace the commented out code below with a neater version using the IAttributeAccessor interface (so that I don't have to write 'if' statements for all control types). At...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
6
by: Pugi! | last post by:
I use mycurrent_cell.setAttribute("style", "background-color:green"); and mycurrent_cell.setAttribute("colspan", "3"); to set some attributes of a table. The code works perfect in FireFox and...
21
by: James Black | last post by:
I am curious if there is a benefit to set attributes directly, in my javascript, or to use setAttribute. For example, I have this: var input = document.createElementNS(xhtmlNS, 'input');...
5
by: gkelly | last post by:
Can someone explain what I am doing wrong, or why this will not work? I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same result. Take for example this code: <html>...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
14
by: Sri02 | last post by:
All, I was trying to add an onclick event using setAttribute to a <tdfrom javascript. Apparently the code doesnot seem to work in FF3 but works pretty well in IE7. Here is the snippet for ur...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.