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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

binding - How can you make the color of elements of a WPF DrawingImage dynamic? -

c# - How to add a new treeview at the selected node? -