user3682205
user3682205

Reputation: 241

using javascript to pass value from a child to parent window

I am trying to pass a value from a child pop up to a parent page through setting element attributes (in this case value) using windows.opener. The script on the child pop up is:

<?php
  if(isset($_POST["Submit"]))
    {
      echo "<script>
      window.opener.document.getElementByName('garage_details[]').value = document.getElementsByName('garage_description[]').value;
      window.opener.document.getElementByName('garage_cost[]').value = document.getElementsByName('garage_cost[]').value;
      self.close();
      </script>";
     }
 ?>

The popup doesnt close when the submit is posted so i am guessing there is an error in how i am equating the values

Upvotes: 1

Views: 622

Answers (1)

epascarello
epascarello

Reputation: 207511

getElementsByName returns an html node collection and you treat it as a single element. You need to select the first element of the set. (note you need to make the change in two places in your code.)

document.getElementsByName('garage_description[]')[0].value; 
                                                  ^^^

If you are expecting multiple inputs with values, you need to loop through the array-like collection.

Upvotes: 1

Related Questions