user1684099
user1684099

Reputation: 139

jquery to change form target value

I need to make a button's target different from the main submit value. I am using jquery with code below:

HTML:

<form action="somepage" id="cartform" target="_parent" method="post">

(So default action is parent)

JQUERY:

$("#refresh").click(function(){
    $("#cartform").attr('target', '_self');
});

("Refresh" is the form's extra button to keep the form in a popup box)

Upvotes: 2

Views: 24816

Answers (1)

Giona
Giona

Reputation: 21114

Try to add a listener on submit instead of click:

$('cartform').on('submit',function(){
   $(this).attr('target', '_self');
});

Obv it's the case only if you always want the target to change as the form is submitted. Otherwise, try with your code and preventDefault.

$("#refresh").click(function(ev){
    ev.preventDefault();
    $("#cartform").attr('target', '_self').submit();
});

I think the form is being submitted before the target change applies.


Edit

Actually, OP is trying to submit a form inside an iframe. This should work:

$("#refresh").click(function(ev){
    ev.preventDefault();
    $("#cartform").attr('target', 'name_of_the_iframe').submit();
});

Will submit to:

<iframe name="name_of_the_iframe" ...

Upvotes: 5

Related Questions