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

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -