Global variable very basic
Hello everyone,
I have a very basic question with global variable and Jquery.
Here i is a counter. I want to know its last value and keep it out of the function
- <div class="aremplir">
- </div>
- <button type="button" id="ajouter">Ajouter</button>
- <script type="text/javascript">
- jQuery(function($){
- var i = 0;
- $('#ajouter').click(function(e){
- e.preventDefault();
- var content = "<div id='input_name"+i+"'><input type='text' /></div>";
- i++;
- $('.aremplir').append(content);
- mon_dernier_i = i;
- });
- if(typeof mon_dernier_i !='undefined'){
- console.log(mon_dernier_i);
- }
- });
- </script>
it seems that mon_dernier_i is not defined : nothing happens. I thought that not declaring 'var' in the function would make 'mon_dernier_i' global but obviously I am wrong...
Can anyone explain it to me ?
Thank you very much