I've encountered an issue while working on setting/getting a hidden input's value using JavaScript, and I can't seem to figure out where the problem lies.
My main objective is to maintain the expandable/collapsable state of divs across form submissions on my page. To achieve this, I placed a hidden input on the page to store the state of the divs. Whenever a div is expanded or collapsed, I update the value of the input accordingly. Upon page load, I retrieve the input value and adjust the state of the divs based on it.
However, it seems that the input value is not persisting. Despite confirming through alerts that it's being set correctly, upon loading, I find that it's empty. Here's the relevant code snippet:
<input type="hidden" name="ECState" id="hdnECState" />
<script language="javascript" type="text/javascript">
<!--
var ecValue;
function ec(div, btn) {
//function to expand or collapse an error detail div
var e = document.getElementById(div);
var b = document.getElementById(btn);
var ecStr = div.toString() + ',' + btn.toString() + '|'
if (e.style.display == 'block') {
e.style.display = 'none';
b.src = '../../Images/plus.gif';
ecValue = ecValue.replace(ecStr, '');
}
else {
e.style.display = 'block';
b.src = '../../Images/minus.gif';
ecValue = ecValue + ecStr;
}
alert(ecValue);
document.getElementById('hdnECState').value = ecValue;
}
function reexpand() {
//function to restore the expanded state of the error detail divs
var pipe, comma, db, div, btn, e, b;
var n = document.getElementById('hdnECState').value;
alert('n=' + n);
if (n != '') {
pipe = n.indexOf('|');
while (pipe > 0) {
db = n.substring(0, pipe);
comma = db.indexOf(',');
if (comma > 0) {
div = db.substring(0, comma);
btn = db.substring(comma + 1);
e = document.getElementById(div);
b = document.getElementById(btn);
e.style.display = 'block';
b.src = '../../Images/minus.gif';
}
n = n.substring(pipe+1);
pipe = n.indexOf('|');
}
}
}
reexpand();
//-->
</script>
Upon expanding a div, the alert from ec() displays that ecValue is 'foo,bar|'.
However, after submitting the form, the alert from reexpand() reads 'n='.
Can anyone spot what might be causing this issue?