views:

347

answers:

1

Hi.

I have a HAML page that lists some links to delete "things" that looks like this

%div
   %a.form_submit Delete Thing 1
   %form{:href=>"#", :id=>'comment_1', :method=>'post', :action=>'/thing/delete'}
      %input{:type=>'hidden', :name=>'thingid', :value=>'1'}
      %input{:type=>'submit', style='display:none'}

%div
   %a.form_submit Delete Thing 22
   %form{:href=>"#", :id=>'comment_22', :method=>'post', :action=>'/thing/delete'}
      %input{:type=>'hidden', :name=>'thingid', :value=>'22'}
      %input{:type=>'submit', style='display:none'}

The intention is to have a link "Delete XX" which will delete something specific. A page could have a number of these links, and each link is for a specific "thing".

I also have a piece of (unobtrusive) jQuery javascript that adds a click handler to each form as follows:

$('a.form_submit').click(function(event) {
    $('form').submit();
    event.preventDefauilt();
});

This works when there is one form on a page. However, if I have more than one form on a page, how do I ensure that clicking "Delete Thing 1" will trigger a submit() event only on the form with id='comment_1'?

+1  A: 

i'm not sure but this might work...

$('a.form_submit').click(function(event) {
    var num = parseInt($(this).text());
    $('form#comment_'+num).submit();
    event.preventDefauilt();
});

i'll also suggest to take out :href=>"#", cause it's not an attribute of a form...

Reigel
This answer definitely got me on the right track.
Jay Godse
might as well choose this as the answer... :)
Reigel
Thanks Reigel. This answer got me the closest to my solution. What I ended up doing was putting an id on the link and then using a replace. $('a.form_submit').click(function(event) { var id=this.id.replace(/comment_/i,''); $('form#comment_'+id).submit(); event.preventDefault(); });That "replace" function let me use a naming convention (comment_) to achieve my goal without having duplicate id's in the link and the form.
Jay Godse