Can't read variable

<script type='text/javascript'> var toid; function reply(id){ var toid = id; // тут изменяем }; </script> 

then execute another script

 <script type='text/javascript'> $(function() { $('#send').click(function(){ $.ajax({ type: 'POST', url: '/send.php', data: {'id': toid, .......... // тут отправляем 

It turns out that the void is sent. Why is the variable not written / read?

  • Understood, removed the second var. did not know. - Rufex

1 answer 1

It is not necessary to declare a variable inside the function again if you want it to be visible outside the function. Announce it once in the global scope

 var toid; function reply(id){ toid = id; // тут изменяем };