Uncaught Typeerror: Cannot set Property 'value' of null

Asked

Viewed 390 times

1

I have a Javascript function and it is returning me the following error:

Uncaught Typeerror: Cannot set Property 'value' of null

if (parseInt(data_inicio) < parseInt(dia_vencimento)) {

  document.getElementById("<%= checkPorp.ClientID %>").checked = true;
  document.getElementById("<%= checkPorp.ClientID %>").style.visibility = "visible";
  document.getElementById("<%= lbValorPor.ClientID %>").style.visibility = "visible";
  document.getElementById("<%= Label20.ClientID %>").style.visibility = "visible";

  var valor = document.getElementById("<%= txtValorServico.ClientID %>").value;
  valor = valor.replace(",", ".");
  var dias = valor / 30;
  var vencimento = dia_vencimento;
  var porpocional = data_inicio - vencimento;
  var qtd = porpocional * dias;
  var resultado = valor - qtd;

  var labe1 = document.getElementById('lbValorPor');
  labe1.innerHTML = resultado.toFixed(2);

  document.getElementById("<%= txtpropor.ClientID %>").value = resultado;
  document.getElementById("<%= lbValorPor.ClientID %>").value = resultado;

}

It gives error in this line:

document.getElementById("<%= txtpropor.ClientID %>").value = resultado;

Only that in the Debugger of the code, the result has value, do not know why it returns me this error.

<asp:TextBox ID="txtpropor" runat="server" Enabled="True" ViewStateMode="Enabled" AutoPostBack="False" Visible="False"></asp:TextBox>

I tried every hint informed in the question: question link, but none solved my problem.

  • Because the id given by <%= txtpropor.ClientID %> does not exist on the page

  • It exists, is on the same html page.

  • I used all the tips of this question, but none helped me.

  • Then work out a [mcve] posting the JS code generated by the application, showing what is the value of txtpropor.ClientID.

  • What is the HTML generated by txtpropor? and if Voce der console.log(<%= txtpropor.ClientID %>) Id is identical to the generated HTML?

  • Yes, but it returns me this error, that because the result has value, I don’t know why it gives the error when passing the value to txtpropor.

  • @Lucascosta the funny, that if I comment this line of txtpropor, the code works as it should work, only q I need to pass the value to that txt,only that it informs this error.

  • He is giving this error because there is error somewhere. If you want we can continue on chat @marianac_costa

  • @Lucascosta we can, if you can help me

  • @Lucascosta I changed the line to this: $("#txtpropor"). val(result); and it worked, only now from the same error, in another, q pass the equal value was being passed before, this is a good practice ? I’m still new to javascript and jquery. Thank you.

Show 5 more comments
No answers

Browser other questions tagged

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