In Rails how do I swap images using the image_tag and JQuery

33 views Asked by At

I'm using rails 6 and ruby 3.1.1 My app is running on Heroku.

I have two images: plus.png, which is a + symbol and minus.png, a - symbol. Both are in assets/images.

When my view loads, the + image shows. When a user clicks on the image I want to change it to the - image - and vise-versa. In my code, I'm just trying to get it to change from + to - to start.

in my view:

<%= image_tag('plus.png', :size => '15x15', :id => "img-white") %>
in my application.js

$('#img-white').click(function(){
  $(this).attr("src", "<%= asset_path('minus.png') %>");
});

I got the asset_path part from another thread on Stackoverflow, but this just produces a broken image in my view.

How can I make this work?

Thanks for any help.

0

There are 0 answers