0

I have a directory page where the listings are rendered on the index page.

<% @listings.each do |listing| %>
  # do some stuff
<% end %>

I've added a data-toggle to each listing - basically a button.

 <a id="chat-menu-toggle" href="#dr" class="chat-menu-toggle" >
   <div class="iconset top-chat-dark ">
      <span class="badge badge-important hide" id="chat-message-count">1
      </span>
   </div>                 
  </a>

This data-toggle opens a STATIC div. The div is a slider with content.

<div id="dr" class="chat-window-wrapper">

I want to use each button to pass the listing.id to a variable. Then, I can use that ID throughout the div.

For example:

Listing with ID:

  • Stack - 1
  • Overflow -2
  • Ruby - 3
  • Rails - 4
  • Onclick - 5

Let's say I click the button for "Ruby" which has an id of 3, I want the id to be passed around like so.

 variable = 3

 <div id= <%= variable %> class="chat-window-wrapper">
miler350
  • 1,411
  • 11
  • 15

1 Answers1

0

Not to sure what you mean by "use that ID throughout the div." but you could try this if it helps you?

<div data-id="<%=listing.id %>" class="chat-window-wrapper">

If you want to get the id's value from what ruby printed then you can try adding an event listener

$(".chat-window-wrapper").on("click",function() { 
    var x = parseInt($(this).attr('data-id');
}

To access listings outside iterator try

listings[index] 

or

@allListings = listings. Then access @allListings[index]

but as far as my rusty ruby knowledge goes, i think listings should be accessible as long as the controller is passing it in the context to the view.

user3413046
  • 109
  • 1
  • 13
  • It's less likely that `listing.id` could be duplicate, so keeping `id` is probably better for DOM querying. I thought the problem is just the quotes around the `id`'s value which you've shown. +1 – vee Mar 12 '14 at 23:22
  • "use that ID throughout the div." => what I mean is, if I can grab that ID as a variable. I can use it in rails queries. ie. Thing.find(variable) – miler350 Mar 12 '14 at 23:26
  • @miler350, how do you plan to execute `Thing.find(variable)`? Is there any ajax call on that click event? – vee Mar 12 '14 at 23:28
  • No, but there probably needs to be. I'm a novice at js. This is how it currently works -- when you click the button, the main content div shifts left ~300px and makes an additional page column. I want to make the content of that column dynamic. The issue is that the div code is outside of the iterator, so I need to grab the id from the iterator, and use it elsewhere on the page. – miler350 Mar 12 '14 at 23:38
  • @user3413046 - I need access to the listing id outside of the iterator. – miler350 Mar 12 '14 at 23:40