Home Ask Login Register

Developers Planet

Your answer is one click away!

Tom Chambers February 2016

How to change the action on a form in a popup?

I have the jQuery included that will change the action of the form on the popup page but I want to have the window opener be able to control the form action of the popup... Is this possible?

popup HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<body onLoad=chgAction();>
<script>
function chgAction() {
$('#search').attr('action', 'myNewActionTarget.html');
}
</script>
<base href="http://vahud.com/">
<form id="search" method="GET" action="doWork.php">
First Name <input type="text" name="fname" value="Tom"><br>
Last Name <input type="text" name="lname" value="Chambers"><br>
<input type="submit" class="form-submit" value="Search">
</form>

This is in my window opener.. and above is the form in the myform.htm that will be the popup.. I want to be able to move the action changer down here to the window opener page.. to get the same result I get with it in the form..

window opener JS:

window.open('myform.htm','mywindow','width=300,height=300');

Answers


mkaatman February 2016

You're almost there. You need to wrap your javascript in a document.ready so that it binds after the form exists.

$(document).ready(function() {
   var popup = window.open('/' + window.URL_PREFIX + '/', '_blank');
   popup.onload = function() {
    $(popup.document.body).attr('action', 'myNewActionTarget.html');
  };
  chgAction();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<body>

  <base href="http://vahud.com/">
  <form id="search" method="GET" action="doWork.php">
    First Name
    <input type="text" name="fname" value="Tom">
    <br>Last Name
    <input type="text" name="lname" value="Chambers">
    <br>
    <input type="submit" class="form-submit" value="Search">
  </form>

https://jsfiddle.net/7gz2uv1w/

Post Status

Asked in February 2016
Viewed 1,296 times
Voted 6
Answered 1 times

Search




Leave an answer


Quote of the day: live life