3

I want to loop over the array lists_indexes to update several lists of values in my HTML page.

Currently, I update a single such list through:

$('#List_1').html("<%= escape_javascript(options_for_select(@values, disabled: @disabled)) %>")

How do I loop over lists_indexes to update List_2, ..., List_n?

2
  • An array, containing the lists indexes. Commented Sep 29, 2015 at 16:30
  • list_indexes = [1, 2, 3, 4..., n] Commented Sep 29, 2015 at 16:31

1 Answer 1

5

What you need is this, you dont need to loop over in CoffeScript, you can loop via ruby

<% list_indexes.each do |i| %>
$("#List_<%= i %>").html("<%= escape_javascript(options_for_select(@values, disabled: @disabled)) %>")
<% end %>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but that's not the answer I expect, because I'm not in a context where I can loop in ruby. I'm not going through a ruby controller, there. The Coffeescript is triggered by a remote script triggered by a view. So there is no way I can loop in ruby. I need to loop in the Coffeescript .js.erb
@cedric You're already in a context where you're using Ruby--what do you think <%= ... %> and escape_javascript are? It's an erb file.
OK, I just realised I could embed Coffeescript within Ruby code, as well as Ruby code within Coffeescript, INSIDE a .js.erb file. I had no idea it was possible to do this, I'm sorry. What is obvious to one is not to another, I'm afraid...Well, so now I learnt something BIG. Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.