PDA

View Full Version : get & set attribute functions



Trinithis
05-20-2007, 09:27 PM
Is there much of a point to using something like:

img.setAttribute("src", "www.mysite.bmp");

vs.

img.src = "www.mysite.bmp";

If there is, why? Is it for private data (if JS has that stuff)? And the samething for getAttribute.

djr33
05-20-2007, 09:31 PM
I'd assume that this is useful when the attribute isn't availabe in the object.attribute method.
This might work for a made up attribute, perhaps, if you needed to store an additional property in the tag. I'm not really sure.

mwinter
05-20-2007, 09:49 PM
Is there much of a point to using something like:

img.setAttribute("src", "www.mysite.bmp");

vs.

img.src = "www.mysite.bmp";

Not usually, no.

Keep in mind that the DOM isn't just for the Web, nor is it only for HTML. The DOM can also be used for manipulating XML documents and the short cut property accessors are not defined in this case; using the set/getAttribute methods is the only choice.

When scripting browsers, avoid the method approach. MSIE doesn't implement them properly.