I need to be able to use a variable from within a loop, outside of the loop to be used in javascript between script tags. Preferably not with script tags and in a file but script tags to start...
I have this that doesn't work:
<% @line_items.each do |li| %>
<button type="button" onclick="mockupColor<%= "#{li.id}" %>()"></button>
<div id="tshirt-div-<%= "#{li.id}" %>">
#code
<script>
function mockupColor<%= li.id %>(){
document.getElementById("tshirt-div-<%= "#{li.id}" %>").style.backgroundColor = '#000000';
}, false);
</script>
<% end %>
Error: mockupColor1 is not defined
This does work:
<% @line_items.each do |li| %>
<button type="button" onclick="mockupColor"></button>
<div id="tshirt-div">
#code
<% end %>
<script>
function mockupColor(){
document.getElementById("tshirt-div").style.backgroundColor = '#000000';
}, false);
</script>
The issue is I need to be able to call for the li.id because once i figure this function issue out, the colors will change depending on the li.id.
Is there a way to somehow match up the li.id from the loop in a script tag outside of the loop?
I don't see why this won't work because I have definitely used javascript within loops before and worked out fine. I used similar code in a fields_for loop in an app and using functions this way worked out perfectly fine...For whatever reason, not this time. Or is there something I am missing and not doing right?
HTML: (inside loop)
<button type="button" class="btn btn-info" data-toggle="modal" data-color="796" data-target="#exampleModal2-796" id="#exampleModal2-796" onclick="mockupColor()">
<script>
function mockupColor() {
document.getElementById("tshirt-div-796").style.backgroundColor = '#000000';
};
</script>