jquery - JavaScript; How do I declare a variable global? -
http://jsfiddle.net/borayeris/6kvyb/
<ul> <li>foo</li> <li>bar</li> </ul> <script> $('li').each(function(index) { var qq=$(this).text(); alert(index + ': ' + qq); }); alert(qq);// asking one. </script>
you've declared qq inside scope of function. once function exits, qq doesn't exist anymore.
if want have alert qq, need declare outside of function. keep in mind contain last value assigned it.
var qq; $('li').each(function(index) { qq=$(this).text(); alert(index + ': ' + qq); }); alert(qq); // alert 'bar'
Comments
Post a Comment