1

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.

obo
  • 1,652
  • 2
  • 26
  • 50

1 Answers1

3

#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'
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173