Is there any method available to add IDs like there is for adding a class - addClass()?
Try this: $('element'). attr('id', 'value');
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.
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.
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.
do you mean a method?
$('div.foo').attr('id', 'foo123');
Just be careful that you don't set multiple elements to the same ID.
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