Javascript: setAttribute() v.s. element.attribute = value to set “name” attribute

蹲街弑〆低调 提交于 2019-11-27 14:42:50

My guess (because you didn't specify the element type) is the element normally does not have a name attribute, so setting the DOM property like that won't work.

For example, setting the name property on an input element will work. Setting it on a div will not.

It will work, however, with setAttribute().

jsFiddle.

To extend the answers provided by some of the others ...

The attribute 'name' is only considered valid DOM for a few specific objects. According to https://developer.mozilla.org/en-US/docs/DOM/element.name those objects are:

 <a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, 
 <map>, <meta>, <object>, <param>, <select>, and <textarea>

For these objects you can set, get and change the name attribute using object.name BUT FOR ANY OTHER DOM OBJECT the attribute 'name' is a custom attribute and must be created using SetAttribute() or by adding it to the HTML declaration. Once it is created, you can acces it using setAttribute() and getAttribute() or you can refer to its value directly using object.attributes.name.value take a look at http://jsfiddle.net/radiotrib/yat72/1/ for an example. BTW - the alert box on load is intentional - check the code to see why ...

Manohar Reddy Poreddy

(Attempting to explain part of the above post a better, separately, since it is already went into -ve rating, and belief in that post will be less. Help improve this further.)

*** The property

When you use, element.name, you are accessing a existing property named "name" or setting its value.

Example 1:
var div1 = document.getElementById("div1"); 
div1.textContent = "2";

*** The attribute

but, while using, element.setAttribute('name','someName'), you are actually setting the attribute named 'name'. This attribute can be an existing property too

Example 2:
var h1 = document.getElementById("H1"); 
h1.setAttribute("class", "democlass");

Example 3:
var d = document.getElementById("d1"); 
d.setAttribute("name1", "value1");

when you use, element.name, you are accessing the property/creating a property named "name" and setting its value.

when you use, element.name, you are accessing the property/creating a property named "name" and setting its value.

but,

while using, element.setAttribute('name','someName'), you are actually setting the attribute 'name'.

IE8 and below treats the property and attribute as same, the bug has been fixed in IE9 and above.
Safari, FireFox, Chrome treat property and attribute differently.

However, you can always create a new property of your choice if you wish to do so.

<head>
<script>
function test($count) {
document.getElementById("test1").setAttribute("name","file-upload_" + $count);
}
</script>
</head>
<body>

<p>some content</p>
<input id="test1" type="file" name="file-upload" id="file-upload" />
<p>some other content</p>
<script>test(1);</script>
</body>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!