I am having an index view with several rows for items which have multiple attributes. I would like to update the content of an items attribute with ajax, therefore I need a unique dom id. Is there a helper method available? What I am doing right now is:
<span id="<%= item.id.to_s + "_on_storage"%>">
Best, Phil
How to add a unique id for an element in HTML? How to add a unique id for an element in HTML? Use the id attribute in HTML to add the unique id of an element. You can try to run the following code to implement id attribute −
The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet.
This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element on your document. Returns null if no elements with the specified ID exists. An ID should be unique within a page.
XSLT's generate-id () function generates a unique ID for a node passed to it as an argument. This ID starts with a letter so that you can use it as the value of an XML ID attribute. For example, the following stylesheet copies an XML document and adds a uid ("unique ID") attribute to each chapter, sect1, and sect2 element.
You can use
dom_id(item)
Which is an ActiveRecord method for generating DOM IDs.
A good practice as well is to use HTML5 data attributes:
<span data-item-id="<%= item.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