jortizromo
jortizromo

Reputation: 824

how do I change the source of image_tag helper using JQuery?

I am using Image_tag helper and the assets pipeline from rails, this way:

<%= image_tag "win_jorge_start_0.jpg", :id => "image0" %>

I am trying to change the source of the image_tag using JQuery like this:

$('#image0').attr( "src" , "win_jorge_complete_0.jpg" );

the src of the image is changed as expected:

<img alt="Win jorge start 0" src="win_jorge_complete_0.jpg" id="image0">

but the source path is not completed as it is when using the Image_tag helper:

<img alt="Win jorge start 0" src="/assets/win_jorge_start_0-51b6339e1e0a021aa878ee8b54cb957a.jpg" id="image0">

is there a way to do this?

Upvotes: 3

Views: 3417

Answers (1)

yaser eftekhari
yaser eftekhari

Reputation: 235

In your JavaScript use:

$('#image0').attr( "src" , "<%= image_path "win_jorge_start_0.jpg", :id => "image0" %>" );

also see: AssetUrlHelper#image_path

Upvotes: 3

Related Questions