Document.getElementById( ).value is not working with window.open( )

Asked

Viewed 582 times

0

I want to generate a clean print page with some information only. For this I am using the following code (well summarized) in javascript:

function myFunction() {
  var myWindow = window.open(" ", "_self");
  var str1 = "Janela de Impressão";
  var result1 = str1.fontsize(5);
  myWindow.document.write("<strong><center>", result1, "</center></strong>");
  var str2 = "Dados da Impressão";
  var result2 = str2.fontsize(5);
  myWindow.document.write("<strong><br><br>", result2, "</strong>");
  var salario_bruto = document.getElementById("salario").value;
  myWindow.document.write("<strong><br>Salário: </strong>");
  myWindow.document.write(salario_bruto);
}
<div>
  Salário: <input id="salario" name="salario" type="text">
  <button type="button" onclick="myFunction()">Imprimir</button>
</div>

I tested this offline and it worked perfectly. But when I went up to the site I click on "print" and don’t pass on the salary amount. Any idea? Thanks.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

6


You are changing windows before you take the value of the element. Just take the value before you give the window.open:

function myFunction() {
  var salario_bruto = document.getElementById("salario").value;
  var myWindow = window.open(" ", "_self");
  var str1 = "Janela de Impressão";
  var result1 = str1.fontsize(5);
  myWindow.document.write("<strong><center>", result1, "</center></strong>");
  var str2 = "Dados da Impressão";
  var result2 = str2.fontsize(5);
  myWindow.document.write("<strong><br><br>", result2, "</strong>");
  myWindow.document.write("<strong><br>Salário: </strong>");
  myWindow.document.write(salario_bruto);
}
<div>
  Salário: <input id="salario" name="salario" type="text">
  <button type="button" onclick="myFunction()">Imprimir</button>
</div>

  • @Sam had not seen that you had answered, it was bad

  • 1

    Quiet! It was for a few seconds. Good answer!

  • 1

    Poutz, I waver mine! Thank you very much!

  • 1

    @Bruno be sure to mark the answer with . Abs!

3

After opening the window using _self, the code no longer finds the id salario page because the page is replaced. Place the line that picks up the input id before opening the window in function:

function myFunction() {
    var salario_bruto = document.getElementById("salario").value; //<< AQUI
    var myWindow = window.open(" ", "_self");
    var str1 = "Janela de Impressão";
    var result1 = str1.fontsize(5);
    myWindow.document.write("<strong><center>",result1, "</center></strong>");
    var str2 = "Dados da Impressão";
    var result2 = str2.fontsize(5);
    myWindow.document.write("<strong><br><br>",result2, "</strong>");
    myWindow.document.write("<strong><br>Salário: </strong>"); myWindow.document.write(salario_bruto);
}

Browser other questions tagged

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