Receive values (popup) on parent page

Asked

Viewed 505 times

1

On another question, they were able to help me about storing the values of some input of a popup, to move to the parent page.

Summary.jsp (father)

<input type="button" onclick="test();" value="Palavras" />
<div id="idPai"></div>

function test(){
  window.showModalDialog('saida.jsp',null,'status:off;center:yes;scroll:no;');
}

In popup, there is a function, addCampos(), for the user to create several fields input.

saida.jsp (popup)

<div id="campoPai"></div>
<input type="text" id='ctxt' name="campo[]" class="form-control">   
<input type="button" value="Adicionar campos" onclick="addCampos()">
<button class="btn btn-success" onclick="enviaDados();">Envia Dados</button>

<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++;
  }
  function removerCampo(id) {
    var objPai = document.getElementById("campoPai");
    var objFilho = document.getElementById("filho"+id);
    //Removendo o DIV com id específico do nó-pai:
    var removido = objPai.removeChild(objFilho);
  }
</script>

In the method below, is the main part of the other question, is where is passed the values (an array) to the parent page.

<script language="javascript">
  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>");
    });
    window.close();
  }
</script>

Updating

My problem is that I don’t know how to use (catch) this value on the parent page. What I need is: the user will type several words in the popup, when closing the popup, the typed words will be compared with a java array, and so only the typed words that are contained in this array will be shown on the screen.

  • 1

    Buddy, you can take the values from the each putting the $(this).val() in an array in the object window using the function Array.push().

  • can you give an example? I’m not used to HTML/Javascript programming

  • 1

    @Peaceful Opa, you need to take input values and play for the parent jsp ( here is meant as the actual rendered html), or for java?

  • @Matheus need to play for the parent JSP. There I will use it in a java function.

  • 1

    @Peace I think it’s a bit confusing, in jsp it means like html with its Javascript functions, if you want to play for java, because it doesn’t use a normal form?

  • @Matheus In short, what I need to do is: Get these words typed in the popup and compare them to a vector that’s in java. I will only show in the page the words that were typed and are in the vector in java. It’s less confusing now?

  • 2

    @Peace Now I understand what you need. But you will have to edit the question, attaching such a problem so they can help you.

  • @Matheus there is some way to buy in real-time the values of each with those of the array java?

  • 1

    @devgaspa Yes, assuming the programmer took the java list and passed it to an array in JS. Then just compare the values of each with the array coming from java.

  • 1

    @Matheus then, suppose there is a array JS values from java, only using if(arrayJsComValoresDoJava.indexOf($(this).val()) != -1) I can verify the existence of the value.

  • 1

    @devgaspa At first yes, and that would solve the question.

Show 6 more comments

1 answer

3

Come on, to get the data you use the same function($(this).val()), within the each, which is assigning the values in the div#idPai of HTML.

To store these values in a array you declare a variable before the function enviaDados. Within the each, every run, you add in array the value using the function push().

See how it looks in the code:

<script language="javascript">
  var arrayDados = []; // declaração do array no escopo global.
  function enviaDados(){
    $("#idPai").html(""); // Aqui irá resetar o conteudo da div.
    $('input[id^="ctxt"]').each(function(){
      arrayDados.push($(this).val()); // adiciona o valor no array.
      $("#idPai").append("<p><b> Valor do Imput: "+$(this).val()+"</b></p>");
    });
    window.close();
  }
</script>

This implementation is to keep the values that came from the modal in a array in the global scope of Javascript, being possible to access the page father.

@Edit

Assuming you’ve created one array in the JS with the JAVA values, as our friend @Matheus said in the comments, this is the global scope array. To verify the existence of the values you can use the function indexOf() to validate.

Obs: Let’s assume you created the array var ArrayJScomValoresDoJava = [x,p,t,o] with the values of array java.

See how it looks in the code:

<script language="javascript">
  function enviaDados(){
    $("#idPai").html(""); // Aqui irá resetar o conteudo da div.
    $('input[id^="ctxt"]').each(function(){
      if(ArrayJScomValoresDoJava.indexOf($(this).val()) != -1) {
        $("#idPai").append("<p><b> Valor do Imput: "+$(this).val()+"</b></p>");
      };
    });
    window.close();
  };
</script>

This implementation will add to div#idPai only when the value passed by the modal exists in the array, read about indexOf if there are doubts.

  • How do I access this vector on the parent page?

  • 2

    @Peace through the variable arrayDados. By the discussion in the comments, perhaps, this example is not feasible for your problem.

Browser other questions tagged

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