0

Possible Duplicate:
JavaScript setAttribute vs .attribute=

Should I prefer one of these two methods to add attributes?

input.name='amount';

input.setAttribute('name', 'amount');
Community
  • 1
  • 1
sid_com
  • 24,137
  • 26
  • 96
  • 187

3 Answers3

4

setAttribute doesn't work correctly in IE

http://www.quirksmode.org/dom/w3c_core.html#attributes

Manse
  • 37,765
  • 10
  • 83
  • 108
3

From MDN:

Using setAttribute() to modify certain attributes, most notably value in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use elt.value instead of elt.setAttribute('value', val).

I guess it would be better to access attributes directly, since that is what the DOM is designed for...

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • 2
    `setAttribute` is a [DOM Element](http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614) method. `.value` is a [HTMLInputElement](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-6043025) attribute defined in the DOM HTML extension. So both are part of the DOM. – Felix Kling Aug 26 '11 at 11:32
0

Mozilla supports only setAttribute(). So if you are designing a multi-browser supporting page, it will be good to have an if condition checking the Browser Name, and call the function/assignments as per that.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • 3
    `setAttribute` *should* work in every browser. It is a DOM element method: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614 Just saying this so that no one gets the false impressions that `setAttribute` is a Mozilla *only* method. – Felix Kling Aug 26 '11 at 11:24
  • 1
    @FelixKling "Mozilla supports only `setAttribute()`" vs. "Only Mozilla supports `setAttribute()`". – glglgl Jun 27 '13 at 09:03