Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6168945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:46:26+00:00 2026-05-23T22:46:26+00:00

Lets assume I have a list of objects in an array and i want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:46:27+00:00Added an answer on May 23, 2026 at 10:46 pm

    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:

    # assuming @items is and array of objects
    <% for item in @items %>
      <script>alert('<%= item.id %>')</script>
    <% end %>
    

    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:

    <script>alert('<%= @items.collect(&:id) %>')</script>
    

    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.

    render :partial => 'item', :collection => @items
    

    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:

    <div id="item_<%= #{item.id} %>" class="item">
      <script>document.write('this is my item id written from JS: <%= item.id %>');</script>
    </div>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array/list/collection/etc of objects. For sample purposes, lets assume it is just
Let's assume I have a list with objects of type Value . Value has
(code examples are python) Lets assume we have a list of percentages that add
So let's assume I have a class named ABC that will have a list
Lets assume I have two hashes. One of them contains a set of data
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
Lets say I have a WCF service that a client can use to receive
Let's say you have a table containing articles and you want want to display
I have serialized a list of objects with protobuf-net. Theoretically, the .bin file can
I have a hierarchical list of objects. Assume that the structure is as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.