Pass values from a popup (child) to parent page

Asked

Viewed 2,170 times

5

The parent window (jsp) has a button to open the popup (child).

<input type="button" onclick="test();" value="Call Child Window" /> 

When you open the popup, there are text fields for the person to type a word. Those input are created dynamically, with a button and a function to create more input. The values typed in input are stored in an array and the qtdeCampos is the amount of fields. The function of buttons:

<script type="text/javascript">
var qtdeCampos = 0;
function addCampos() {
   var objPai = document.getElementById("campoPai");
   var objFilho = document.createElement("div");//Criando o elemento DIV;
   objFilho.setAttribute("id","filho"+qtdeCampos);//Definindo atributos ao objFilho
   objPai.appendChild(objFilho);//Inserindo o elemento no pai
   //Escrevendo algo no filho recém-criado:
   document.getElementById("filho"+qtdeCampos).innerHTML = "<input type='text' id='ctxt"+qtdeCampos+"' name='campo[]'>\n\<input type='button' onclick='removerCampo("+qtdeCampos+")' value='Apagar campo'>";
   qtdeCampos++;
}    
</script>

I would like to know how to pass this array of words typed in the popup to the parent page. The parent page will receive you in an array.

  • 1

    Do you really need to open a new window? Wouldn’t it be better with a dialog?

  • @Sergio believe so, because it can be several word insertions! There is a function in the popup that allowed to open several text fields.

  • In the popup there is a text input that the client will type something. You want to take this value and send it to the jsp that called it. Where exactly do you want to ship to? To a div q exists in the parent jsp? To another input?

  • @Matheus for a div that exists in the parent JSP.

  • @Peace One more thing, send when the user is typing or when the user exits the input or when the user clicks a "send"?

  • @Matheus by clicking send button.

  • 1

    @Peace, all the answers already answer your question Como passar dados do popup para a página pai, What you need now is to understand the answers. In all the answers the value of the popup was saved somewhere on the parent page, now what you need is to know how to manipulate this value on the parent page, think you need another question or just look closer at the answers that have already been given to you.

Show 2 more comments

3 answers

5

You can do it with pure HTML. Just set the attribute target as _parent on your form:

<form target="_parent"></form>

This way the form will be submitted in the parent window, that is, the window through which you opened the pop-up.

5


Assuming you have these inputs in the popup:

<input type="text" id="email" name="email" class="form-control">    
<input type="text" id="senha" name="senha" class="form-control">    
<button class="btn btn-success" onclick="enviaDados()">Envia Dados</button>

When you click the Send Data button, the function defined in onclick will be called, and it will be sent Data(), where the function will take the input values and put in the div that you set in the parent jsp.

<script>
  function enviaDados(){
    var email = $("#email").val();
    var senha = $("#senha").val();

    //insere os valores nas div pai pelo id da div que você definir.
    $("#div1").text(email);
    $("#div2").text(senha);
  }
</script>

This solution was used Javascript and Jquery.

Edit

Supplementing the answer after the amendment of the question.

In the new function, jquery will run all inputs with id ctxt, taking the value of each input and inserting in the div with the id "idPai".

function enviaDados(){
    $("#idPai").html(""); // Aqui irá resetar o conteudo da div.
    $('input[id^="ctxt"]').each(function(){
        $("#idPai").append("<p><b> Valor do Imput: "+$(this).val()+"</b></p>");
    });  
}

Example: https://jsfiddle.net/p0L2g7wp/1/

  • How I would receive these values on my parent page?

  • @Peace He takes the values of the variables email and password, and plays in a div q tu definio on the parent page

  • I still don’t understand how the code will be on the parent page. Besides, I updated the question, because I have an array with the answers.

  • @Peace It is not yet clear, which element on the parent page you want to send ( in a div, in a text field ), this is vague in your explanation. Set exactly what element on the parent page you want to send, so I can edit the responsta.

  • In a div same. Please update the reply with an example of the parent page. And don’t forget that I changed the question, showing that it is a vector with multiple "answers" of inputs

  • 1

    @Peace In a div eh very vague, because it looks soh, I will play only the text within this div? No right. So define more, example: I want the input data inside a div with id="parent", where each input value should be inserted inside a <p tag>.

  • this is a problem tbm. As I do not know how to return the values, I do not know how to receive them on the parent page. It may be that each input value is inserted within a <p> tag, what I need at the end is that (as my page is JSP) I can put this array that comes from the popup into an array on the parent page. I don’t know if I could express myself well. I could understand?

  • 1

    @Peaceful Managed to solve the problem?

  • I even figured out how to send it to the parent page, but I still don’t know how to get this div on the parent page. Anyway, for such a question, your answer was the most complete. I will ask another question to understand how to manipulate on the parent page.

Show 4 more comments

4

Use the property window.opener.Document to manipulate any element on the parent page.

var $botaoFechar = document.getElementById('botaoFechar');
$botaoFechar.addEventListener('click', function () {
    //Elemento na página chamadora que será adicionado o valor da filho
    var $meuTextBoxPai = window.opener.document.getElementById('Ptxt');
    //Elemento do popup com os dados a serem transferidos
    var $meuTextBoxFilho = document.getElementById('ctxt');

    $meuTextBoxPai.value = $meuTextBoxFilho.value;
    $meuTextBoxPai.focus();
    window.close();
});

Explaining the example:

  • I added a click event to the element with id meuBotao
  • Looking for an element with id meuTextBox (you can manipulate any element) of the calling page (parent)
  • I assign this element the value I want
  • Then I assign the focus to it
  • And finally, I close the popup
  • Okay, that’s code that would be in my popup. But how does the parent page look??

  • 1

    What do you want to do on the parent page? I gave you an example of how to send any data from your popup to anywhere on your calling page. Now you decide where to send this information and what to do with it. Simple as this.

  • The problem is that an array I want to pass to the parent page. Does it work the same way?? And about what to do on the parent page, I need to put in a vector.

  • 1

    @Peaceful, I edited the question using the fields with the same name you used in the other question. Try not to repeat the same question, you can edit your question as many times as necessary to provide more information.

  • I edited the question to get better with a better understanding about my doubt. See q in your answer, you take document.getElementById('ctxt'); and in my function each id is ctxt+numero.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.