binding event to form in window.open

I am trying to open a pop-up window which has a form. Eventually the user will close the window, but before that I want to send a form value back to the parent window using localStorage. If I use dev-tools to execute my javascript commands I am successful. So I don't think it's a problem  there. I suspect the problem is I am not able to bind the localStorage.setItem to the form on the pop-up.
Here are the relevant bits of code from the parent window.
        // onload
        j$(document).ready(function() {
            // disable ajax request caching for security best practice
            j$.ajaxSetup({ cache: false });

            // open modal and query available record types when page loads
            openModal();
            
            //
            j$(".modalSubmit").click(function(event) {
                validateRecordTypeSelected();
            });

           window.addEventListener('storage',function(event) {
               console.log(event);
           });

        });

Open in new window


and
        function openModal2() {
           console.log('running openModal2');
           var popup = window.open("/apex/wrapwiz", "myWindow", "width=600, height=400, toolbar=no, menubar=no");
           j$('popup, #j_id0\\:j_id1\\:i\\:f').bind('submit', function( event ) {
              alert( "Handler for .submit() called." );
             localStorage.setItem(document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input').value);
                                                          
              });
         }

Open in new window


openModal2 is being called by a button on the screen and I know that works because the pop-up opens when the button is clicked.

Here are what I believe are the relevant parts of the pop-up window
<form id="j_id0:j_id1:i:f" name="j_id0:j_id1:i:f" method="post" action="https://c.cs14.visual.force.com/apex/wrapwiz" enctype="application/x-www-form-urlencoded" onkeypress="return FlowLoadingScreen.noenter(event);" onsubmit="FlowLoadingScreen.showLoadingScreen(this)">

Open in new window


and

<td class="labelCol requiredInput"><label class="FlowText" for="j_id0:j_id1:i:f:pb:d:Company.input" id="j_id0:j_id1:i:f:pb:d:Company.prompt"><span class="assistiveText">*</span>Account Name</label></td><td class="dataCol col02"><div class="requiredInput"><div class="requiredBlock"></div>

<input  id="j_id0:j_id1:i:f:pb:d:Company.input" maxlength="255" name="j_id0:j_id1:i:f:pb:d:element___input____Company" size="20" type="text" value="" /></div></td><td class="labelCol last empty">&nbsp;</td><td class="dataCol last empty">&nbsp;</td></tr>

Open in new window


If there is a better way to accomplish my objective, I'm open to scrapping this.

Thanks,
Mike
LVL 3
mmooreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
Hi Mike,

Do you want to submit the form values back to the parent on popup close or/and on form submit, If this is the case you can call a JavaScript method of the parent like this:

/* Pop-up window code */
function submitDataToParent(data){
  if(window.opener != null){    
    window.opener.methodOfParent(data);
    top.close(); /* As the pop-up task is done so, if you want to close the pop-up use this statement. */
  }
}

/* Parent window code */
function methodOfParent(data){
  console.log("Data: ", data); 
}

Open in new window


You need to move this submit event binding code to pop-up this event binding won't work from the parent:

j$('form element selector').bind('submit', function( event ) { /* replace the "form element selector" with the valid form element selector */
  alert( "Handler for .submit() called." );
  /*localStorage.setItem(document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input').value);*/
  submitDataToParent(document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input').value); /* Add this to send data back to parent */
});

Open in new window

0
Leonidas DosasCommented:
Did you serialize the values of the form into string using JSON.stringify() before close the modal;And after when you want to get these values did you parsing via JSON.parse()  the data?
0
Julian HansenCommented:
A few things

1. your localstorage call does not include a value to set. Should be
localStorage.setItem('key', 'value');

2. I would put the javascript code in the popup
<script>
var frm = document.getElementById('j_id0:j_id1:i:f');
frm.addEventListener('submit',function(evt) {
	evt.preventDefault();
	localStorage.setItem('value', document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input').value);
});
</script>

Open in new window


3. bind has been deprecated in favour of .on.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mmooreAuthor Commented:
Hi experts, thanks for the answers. I will be studying and working with each of them over the next 24 hrs. I'll be back.
0
mmooreAuthor Commented:
This is what I came up with.
This is the visualforce page (Salesforce) that wraps the wizard.
<apex:page showheader="false" sidebar="false">
<flow:interview name="New_Account_Wizard"></flow:interview>
<!-- New_Account_Wizard is a multi page flow with a form on each page -->
<!-- so at different stages of the flow the DOM will look different -->   
<script>
   window.onload = function () {
   
     var frm = document.getElementById('j_id0:j_id1:i:f');
     // do this only if the target FORM is on the curent page
     if (typeof(frm) != 'undefined' && frm != null)
     {
     
        var accountNameField = document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input');
        // do this only if the target accountNameField is on the curent page
        if (typeof(accountNameField) != 'undefined' && accountNameField != null)
        {
           // Load the local Storage when the user hits the NEXT button. NEXT button does SUBMIT and goes to next page
           frm.addEventListener('submit',function(evt) {
               localStorage.setItem('AccountName', document.getElementById('j_id0:j_id1:i:f:pb:d:Company.input').value);
           });
        }
     }
     
     var finishBtn = document.getElementById('j_id0:j_id1:i:f:pb:pbb:finish');
     if (typeof(finishBtn) != 'undefined' && finishBtn != null)
     {
        // override the FINISH button on the last screen of the wizard. If you don't,
        // the natural behaviour is to restart the wizard at page one.
        finishBtn.addEventListener('click',function(evt2) {
            evt2.preventDefault();
            window.close();
        });
     }
   }
</script>                
</apex:page>

Open in new window


This is the bit from the Parent window that captures the event and stuffs it in a field
           window.addEventListener('storage',function(event) {
               if (event.key == 'AccountName') { ;
                  j$(".accountNameField").val(event.newValue);
                }
           });

Open in new window


Suggestions for improvements are welcome.
0
Julian HansenCommented:
What is the result of running this code? Does it work?
0
mmooreAuthor Commented:
Yes, it works! It's not clear how to close this issue. I clicked on Best Solution and Assisted Solution for another. Oh wait, I now see a Next button at the bottom of the screen.
0
mmooreAuthor Commented:
Thanks for the help.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.