[EDIT: I tried to put only the relevant portion of my code here. I understand that led to confusion. Sorry about that. Here is a snippet I could create causing the same issue. Adding the snippet here:]
Once snippet is loaded, click on the button.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hello">
Some text
<script>
jQuery(document).ready(function () {
func();
});
var a = 5;
function func() {
alert("Value is FIVE. Integer value: " +a);
}
</script>
</div>
<div id="mybtn" style="border: 2px solid black; padding:5px; background-color: yellow; width: 100px">CLICK</div>
<script>
var data = " \
Some text2 \
<script> \
jQuery(document).ready(function () { \
func(); \
}); \
var a = 6; \
function func() { \
alert('Value is SIX. Integer value: ' +a); \
} \
<\/script> ";
$('#mybtn').on('click', function(e) {
$('#hello').html(data);
});
</script>
I expected to see SIX 6 (or maybe FIVE 5) on clicking the button. But see SIX 5. Interesting that the new function is being called but old value (5) is being used. In my actual code, I had my func() way below where I was calling it and was also calling it in a settimeout(func... ). Trying to understand what is happening.
scripttag is just here in the example code, right...?var1 = 4so why would you expect that to be printed?scriptchanging but the actual JavaScript doesn't update. You'd have to "refresh" thescripttag to solve this. If you could reproduce this in a fiddle I'd gladly show you a solution