I have a website that has a grid with multiple posts on one page. I'm trying to write a javascript function that changes the html on a specific post on the page.
I have this inside my <% @posts.each do |post| %>
loop
<script type="text/javascript">
var ids = '#<%= song.id %>';
function tagInfo(tag) {
$(ids).html(tag);
}
</script>
This is an onClick function. The only problem is, the function isn't getting the ID correctly. When I click on the button, nothing happens. If i change it ids variable to say "#5" it works on the fifth post. But when I use the erb tag it doesn't work.
When I inspect the page though, it the script looks like this
<script type="text/javascript">
var ids = '#5';
function tagInfo(tag) {
$(ids).html(tag);
}
</script>
So it should be working... any suggestions. I've tried <%= raw song.id %> but that doesn't work.
Helper function with the onClick handler
module SongsHelper
def tags(tag_list)
markup = ""
tag_list.each do |tag|
markup += content_tag(:a, tag, :class => tag, :onClick => 'tagInfo("' + tag +'");')
end
raw(markup)
end
end