title: Global variable in javascript result is always undefine
body: <p>I had trouble with my global variable hope you can help me.</p>
  
  <pre><code>&lt;li&gt;
   &lt;a href="&lt;?php echo site_url("adminController/questionAdd/".$row-&gt;subjectid); ?&gt;" id="&lt;?php echo $row-&gt;subjectid; ?&gt;" class="subject"&gt;Add Question&lt;/a&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>Now from that line I passed my id in this line by <code>click()</code> in javascript</p>
  
  <pre><code>$(document).ready(function () {
          var correctAnswer;
          var subId;
  
          $( ".subject" ).click(function() {
              subId = ($(this).attr('id')); //passed the id variable into the global variable
              alert(subId) // when I alert this it returns the value
          });
  </code></pre>
  
  <p>now I used the global variable in this line the same    $(document).ready(function ()</p>
  
  <pre><code>$('#form-user').submit(function(e){
                  e.preventDefault();
  
                  var me = $(this);
                  var correct = correctAnswer;
                  var passedSubId = subId; // passed the global variable to this local variable 
                  console.log(correct); // this is okey
                  console.log(subId); // this is undefined
  });
  </code></pre>
  
  <p>result</p>
  
  <pre><code>i
  undefined
  </code></pre>