Lets assume I have a list of objects in an array and i want each of those items to have an id, let us say data-id="<%= item.id%>" how can i pass this data-id in in JavaScrpt so that it displays an alert message with the items id.
Lets assume I have a list of objects in an array and i want
Share
I’m not sure exactly what you are asking, but if you want to iterate over an array and display alerts for each id it would look something like this:
To address @mikeycgto’s point of a ton of alerts, if you wanted to just have an alert with all the id’s at once, you could do:
EDIT:
So if you want to pass the id along and use the id inside of your item partial for your js, you can use render partial with a collection.
This will render “items/_item.erb” and pass the local variable ad to the template for display. An iteration counter will automatically be made available to the template with a name of the form partial_name_counter. In the case of the example above, the template would be item_counter.
So in your _item.erb partial you could access the item like this:
This would render blocks with div ids in the form of “item_” with a class item, and the js would write out the id inside the block. You can repurpose the js to do whatever you need.