Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a unique dom id for a html tag from model

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

like image 229
dc10 Avatar asked Aug 05 '12 14:08

dc10


People also ask

How to add a unique ID for an element in HTML?

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 −

What is the id attribute in HTML?

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.

What is the use of id method in HTML?

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.

How do I generate a unique ID from an XML document?

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.


2 Answers

You can use

dom_id(item)

Which is an ActiveRecord method for generating DOM IDs.

like image 65
Erez Rabih Avatar answered Oct 05 '22 04:10

Erez Rabih


A good practice as well is to use HTML5 data attributes:

<span data-item-id="<%= item.id %>">
like image 33
iltempo Avatar answered Oct 05 '22 02:10

iltempo