0

I did some code with jquery that take the name attribute !!! OF A DIV !!! and use it in some way.

if in one hand i tryed this code in every browser i have (firefox, chrome, ie 9/8/7, safari, opera, android... all with last update) and it work perfectly, in the other hand i see that - searching with google - "name" is considered only into form, input, textarea, a and other similar.

can i really use it in the way i did? in every html tag I want? or I'll burn forever in the hell?

thank you in advance


seen your answers, I decided to store datas in other way (like adding class and retrieve their name even with jquery).

1
  • Its use in an a element is also redundant, use an element's id for in-page navigation. Commented Mar 5, 2013 at 15:05

2 Answers 2

5

You can use whatever attribute name you want - but 'name' isn't a valid attribute on the div tag according to the specification, so your markup would technically be invalid.

If as an example, you take this html:

<!DOCTYPE html>
<head>
   <title>test</title>
</head>
<body>
   <div name="mydiv" class="myclass"></div>
</body>

And validate it here, you'll see that you get this error:

Line 6, Column 41: Attribute name not allowed on element div at this point.
       <div name="mydiv" class="myclass"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, i didn't realize that there are the validators from w3.org too
2
<a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>, and <textarea>

https://developer.mozilla.org/en-US/docs/DOM/element.name

To the best of my knowledge, name is mostly used for pairing elements with labels (and in that vein, grouping radio buttons).

EDIT: Mentioning Mike's comment. Name is also used for form submission with GET and POST in a key/value pair sort of way.


Update: It may be worth looking at this post.

OMG Ponies has some good info on the topic:

My understanding is that when Netscape created Javascript, it used the name attribute. The HTML spec however decided to go with id, but kept name for backwards compatibility. IME, using the name attribute was required for Internet Explorer 6 support because the javascript engine in IE wouldn't read the id attribute - only the name though both were defined.

2 Comments

i believe the 'for' attribute is what you use for pairing labels with elements. the name attribute is what is used by the browser to set the post variables on form posts.
Well, it'll be <label for="example"></label> and <input type="text" name="example" />, but you are right, name is also used for the key/value pairs on POST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.