Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addID in jQuery?

Is there any method available to add IDs like there is for adding a class - addClass()?

like image 437
eozzy Avatar asked Nov 01 '09 17:11

eozzy


People also ask

How to add id name in jQuery?

Try this: $('element'). attr('id', 'value');

How to give id to element in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element.

How to add object in list in jQuery?

Using append() method: The append() method in jQuery is used to add a new element at the end of the selected element. Parameter: This method accepts single parameter element which need to be inserted. Return value: It does not return anything. Example: This example uses append() method to add new element.


2 Answers

ID is an attribute, you can set it with the attr function:

$(element).attr('id', 'newID'); 

I'm not sure what you mean about adding IDs since an element can only have one identifier and this identifier must be unique.

like image 135
Christian C. Salvadó Avatar answered Sep 18 '22 22:09

Christian C. Salvadó


do you mean a method?

$('div.foo').attr('id', 'foo123'); 

Just be careful that you don't set multiple elements to the same ID.

like image 27
scunliffe Avatar answered Sep 21 '22 22:09

scunliffe