<input type ="button" name="buttonsel" id="buttonse1"/>
$("#buttonse1")
VS $("[name=buttonse1]")
What are the advantages in using name and id in jquery
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.
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.
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.
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.
EDIT
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.
Selecting by ID is still faster than by names in jquery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With