I have the following hunk of JS where I define var i with some PHP.
<script type="text/javascript">
var i = <?php echo $photo_count; ?>;
function updatePreview(){
var x = document.getElementById('preimage').value;
var y = document.getElementById('precapt').value;
var preview = "";
var special = document.getElementById('special').value;
i++;
preview += "<div id='smallbox"+i+"' class='smallbox'><table><tr><td rowspan='2'><img id='picture"+i+"' src='"+x+"' /><br /><input type='button' onclick='removeimg("+i+");' value='delete' /></td><td>URL: <input onchange='updateimg("+i+");' type='text' id='image"+i+"' value='"+x+"'/></td></tr><tr><td>Caption: <input type='text' id='capt"+i+"' value='"+y+"'/></td></tr></table></div><hr />";
//window.alert(preview);
//document.getElementById('special').value += "#"+x+"|"+y;
document.getElementById('preview').innerHTML += preview;
document.getElementById('preimage').value = "";
document.getElementById('precapt').value = "";
//window.alert(document.getElementById('special').value);
}
function showSubmit(){
window.alert("i = "+i);
document.getElementById('hideImg').style.display = "none";
document.getElementById('hideTags').style.display = "block";
while(i>1){
var img = document.getElementById("image"+i).value;
var capt = document.getElementById("capt"+i).value;
if(img.length>3){
document.getElementById('special').value += "#"+img+"|"+capt;
window.alert(document.getElementById('special').value);
window.alert("i = "+i);
document.getElementById('preview2').innerHTML += "<img class='postpreviewimg' src='"+img+"' />";
}
i--;
}
document.getElementById('preview').style.display = "none";
}
</script>
In the page's source I can see that PHP is defining i correctly but in the first line of showSubmit() where I alert(i) it shows i's value to be 1, unless updatePreview() is called first.
In other words.. if the pages source looks like this: var i = 36; and then I call the function showSubmit() it alerts i = 1 when it should alert i = 36
Each time updatePreview() is called i will increment from 1, instead of from 36.
Am I defining i incorrectly? Isn't that the proper way to set a global variable?
ivalue in a console?console.log(i);throughout the script and have a closer look in thedeveloper viewin Google chrome. I am sure you will find what you are looking for :)showSubmit()just before</script>it would also alert the correct value. It changes in another script on the page. What else are you including?