1

I have a rails block like this -

  <% if blah%>
    <% foo(100).each_with_index do |data| %>
      <img src="<%= data %>" onclick="yay(<%= data %>)">
   <% end %>

And yay() should log the value of data.

However, the dynamically generated elements from the rails method foo() don't work with onclick. A static image does. Is there some way around this?

1 Answer 1

2

You need to surround the <%= data %> with single quotes like so:

<img src="<%= data %>" onclick="yay('<%= data %>')">

Otherwise, it will treat it like a JavaScript variable which will cause it to fail.

Check out the console of your browser and you should see a message complaining about this.

Sign up to request clarification or add additional context in comments.

Comments

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.