Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using name vs id in jquery [closed]

Tags:

jquery

<input type ="button" name="buttonsel" id="buttonse1"/>

$("#buttonse1") VS $("[name=buttonse1]")

What are the advantages in using name and id in jquery

like image 657
Ghostman Avatar asked Nov 02 '12 07:11

Ghostman


People also ask

Does jQuery use id or name?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

What is the difference between id and name?

id is used to identify the HTML element through the Document Object Model (via JavaScript or styled with CSS). id is expected to be unique within the page. name corresponds to the form element and identifies what is posted back to the server.

Whats the difference between name and id HTML?

The name Attribute This attribute is associated with the data within the element. Like the id attribute, the name attribute must begin with a letter and is case sensitive, but unlike the id attribute, it can be not unique. The name attribute cannot be referenced in CSS.

Why id selector is faster than class?

As you said, the ID is the faster lookup, but which one you chose has more to do with what you want to do. Think of a class like a category, or classification for different items that have a common or shared aspect to them, while an ID in contrast, has some unique property it is one of a kind.


2 Answers

EDIT

  • The names in name= must be unique within a form. The names in id= must be unique within the entire document.
  • JavaScript needed unique names, but there were too many documents already out here without unique name= names, so the W3 people invented the id tag that was required to be unique. Unfortunately older browsers did not understand it. So you need both naming schemes in your forms.

check full details over here : http://mindprod.com/jgloss/htmlforms.html#IDVSNAME


Id can able to indentify element unique.

valid html contains unique id for each element.

<input type ="button" name="buttonsel" id="buttonse1"/>
<input type ="button" name="buttonsel" id="newbutton"/>//valid for both id and name
<input type ="button" name="buttonsel" id="buttonse1"/>//not valid as id repeated but name is valid even if repeated

But you can give same name to multiple items that might not able find element unequally.

like image 186
Pranay Rana Avatar answered Nov 11 '22 05:11

Pranay Rana


Selecting by ID is still faster than by names in jquery.

like image 29
NullPoiиteя Avatar answered Nov 11 '22 07:11

NullPoiиteя