obo
obo

Reputation: 1662

Rails 3.1 CoffeeScript/JQuery - How to change the source of an image in a div?

I would like to change the image of a link with jquery. This is what I tried :

In my Haml file :

.selected
  = link_to image_tag('/assets/unselected.png'), {:controller => Posts, :action => "update", :selected => true}, :id => "selected_post", :remote => true, :method => :put

After some stuff in my posts_controller, here is the update.js.coffee where I tried to change the image of the link with a new one called selected.png :

$("selected_post").attr 'src', '/assets/selected.png'

I does not work and if I call an alert with :

alert $("selected_post").attr 'src'

It sends "undefined".

I think I am missing something.

Upvotes: 1

Views: 1605

Answers (1)

Dane O'Connor
Dane O'Connor

Reputation: 77278

#selected_post should be your selector

# is the css shortcut for the id attribute. So this should work:

$("#selected_post").attr 'src', '/assets/selected.png'

Upvotes: 3

Related Questions