Setting a _blank goal for only one form submit action

I submit the form to my rails application with two submit buttons. They both go to the same controller action and use the same form, but with one submit action I want to make the target=_blank form so that the results of the form submission open in a new window.

Is there an easy way to do this?

Performing this action for both actions:

 <%= simple_form_for @thingy, :target => '_blank' do |f| %> 

I crashed with jQuery to set an onclick form target, but no luck.

+6
source share
3 answers

On one of the buttons add the say popup class. Then add some jQuery:

 $('.popup').click(function(e) { e.preventDefault(); //prevents the default submit action $(this).closest('form').attr('target', '_blank').submit(); }); 

Demo: http://jsfiddle.net/HxrzD/

Please note that there are other ways to submit the form you may require, f.ex enter . You may also need to reset the target attribute of the form if the user returns to the parent window and is sent again using another button. In this case, you can either simply clear the target at the end:

 $(this).closest('form').attr('target', '_blank').submit().removeAttr('target'); 

Or save the previous state:

 var $form = $(this).closest('form'), target = $form.attr('target'); $form.attr('target', '_blank').submit().attr('target', target); 

http://jsfiddle.net/HxrzD/2/

+8
source

you can intercept a click on a button, read its data and change the form before sending:

So HTML:

 <form id='double' method="GET" action="http://google.com/search"> <input name="q" type="text"> <button class="Submit" data-target=""> Submmit here</button> <button class="Submit" data-target="_blank"> Open new</button> </form> 

Js

 $('button.Submit').click( function() {   var t=$(this);   var form=t.parents('form');   form.attr('target',t.data('target'));   form.submit();   return false; }); 

this way you can control the target parameter in your html markup.

http://jsfiddle.net/oceog/gArdk/

if you do not clear the target form, you will get the following script:

  • the user clicks on the popup button,
  • sent the form
  • closed window
  • click on non-popup

, and it will also put him on target.

so in my snipplet i clear the target in case of data-target = ''

if you want to mark only one element as a popup, you will need to clone your form: http://jsfiddle.net/oceog/gArdk/2/

JS:

 $('button.Submit.popup').click( function() {   var t=$(this);   var form=t.parents('form').clone(true).attr('target','_blank');     form.submit();   return false; }); 

HTML:

 <form id='double' method="GET" action="http://google.com/search"> <input name="q" type="text"> <button class="Submit"> Submmit here</button> <button class="Submit popup"> Open new</button> </form> 
+1
source

To handle all submit cases (and not just clicks), I would do something like the following:

 $("form").submit(function(e) { if($(e.srcElement).is(".popup")){ $(this).attr("target", "_blank"); } }); 
0
source

All Articles