Rails link_to image_tag - getting the href value on click with jQuery -
rails 2.3.5
for link_to image_tag', i've been trying figure out how grab href jquery. assume has nothing rails , me doing wrong using jquery. examples i've found link indicate there should href attribute when grab link @ click time. if put id inside image_tag block or other way around (or :id in both blocks). like, neither of these work (the alert returns 'undefined'):
<%= link_to image_tag("apple.png", :size => "60x50", :alt => "new apple", :id => 'new_apple', :class => 'item_images'), :controller => :items, :action => :new, :item_type => 'apple' %> $('#new_apple' ).click(function(){ alert($(this).attr("href")); ... }
or
<%= link_to image_tag("apple.png", :size => "60x50", :alt => "new apple", :id => 'new_apple', :class => 'item_images'), :id => 'my_new_apple' :controller => :items, :action => :new, :item_type => 'apple' %> $('#my_new_apple).click(function(){ alert($(this).attr("href")); ... }
basically i'm trying grab href use in ajax request (kind of how can grab button submit action , use it's url , type request. haven't found example of structure ajax request based off link click yet (still looking).
thanks help!
the second excerpt should work you're missing closing quote , link_to
wrong:
<%= link_to(image_tag("apple.png", :size => "60x50", :alt => "new apple", :id => 'new_apple', :class => 'item_images'), { :controller => :items, :action => :new, :item_type => 'apple' }, :id => 'my_new_apple') %> $("#my_new_apple").click(function() { alert($(this).attr("href")); }
you can try first link_to
:
$("#new_apple").click(function() { alert($(this).parent().attr("href")); }
Comments
Post a Comment