I have this code in jQuery..
$(document).ready(function(){
var fieldCounter = 0; ...
I have a jQuery function that increments this value.
This works, but I can’t access this value on the page from a non-jQuery function? The reverse is also true, if I scope it in JavaScript e.g.
<script type="text/javascript">
var fieldCounter = 0;
I can access it from javascript but jQuery can’t view it?
I’m probably doing something really dumb?
It has nothing to do with jQuery, but all with Javascript scope.
fieldCounteris declared inside afunction. Since Javascript has function scope, the variable is not visible outside the function.BTW, jQuery is Javascript, they play by the same rules, they’re not two different technologies.
Exhaustive answers can be found here: What is the scope of variables in JavaScript?