1

Today we can writhe huge web pages that contains many user controls, jquery plugins and a lot of html elements.
Sometimes there is a jquery plugin that create a div and assign Id to it. When using this kind of plugin more then one time - the is situation where two elements has the same id.

Does using "name" is better then using "id"?

Naor
  • 23,465
  • 48
  • 152
  • 268
  • I suppose that using the name attribute is way too slow in jquery, as It relies a lot on DOM search. Id is faster, but in your case.... I don't know – Totty.js Oct 04 '11 at 08:14

4 Answers4

3

Plugin that generates same ID for DOM elements by design is not worth using - it shows poor programming. Good plugins will always use unique ID, for example by adding some index.
So, just don't use such plugins.

Name is not really relevant for elements outside of a web form - for example <div> should not have name, this attribute is meaningful only for form elements and in this case yes, better use it as you can give same name to more than one element.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

Id and Name have different meanings in the html / DOM standard. Please have a look at the question Difference between id and name attributes in HTML for more info :)

Community
  • 1
  • 1
bang
  • 4,982
  • 1
  • 24
  • 28
1

There should be no two elements with the same id on a page. For example when you use an id Selector in jQuery like this

 $('#id')

it always returns only one element, regardless of the number of elements with the same id. If you must clone elements, use elements with classes or gerate ids dynamically.

Name is important when you submit data: if two elements have the same name only the latest will be posted, unless you use tha array notation supported by php:

<input type='checkbox' name='ids[]' value='3'>
<input type='checkbox' name='ids[]' value='6'>

in this case PHP would receive an array server side

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

I believe using classes for jquery elements helps avoid a lot of issues.

Using names, especially with ASP.NET, can cause issues if the controls are ran server side, and therefore rendered differently in the HTML.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • So you suggesting using class over using name? But this is not the purpose of the class attribute..Why class is better then name in your opinion? – Naor Oct 04 '11 at 08:17
  • When using jQuery its quite common to use classes for referencing controls. I find this best as you can reference including the parent classes to ensure only the correct element is being referenced. E.g `$(".container .left_column .widget").plugin();` – Curtis Oct 04 '11 at 08:28