javascript - How to declare and set a variable in jquery? -
i have basics question. in jquery if declare variable , assign value inside function, how can maintain change? thought overwrite it. please help.
this simple example of mean. alert box shows 2 how can save 3 in x??
<script> var x=2 $(document).ready(function(){ (function(){ x=3; }); alert(x); }); </script>
you're written function expression, it's not part of iife, it's not getting executed. have put () after function expression execute it:
var x = 2; $(document).ready(function() { (function() { x = 3; })(); alert(x); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments
Post a Comment