Javascript function works only the first time it's called
-
Hi Hope someone can help with this as I'm feeling a bit stupid at the moment! I've got a form (using a master page and an update panel in case it's relevant). This is also (and unfortunately needs to) running in IE6. On there are 3 text boxes which are pre-populated server side. When the value of one of these changes, I need to update another part of the screen (client-side, this doesn't need to go back to the server). So, each of those text boxes has:onFocus="storeValue(this)" onBlur="leavingField(this) so that on entry to the field the value is stored in a global variable and on exit this is checked and actioned if it has changed. The first time you enter and exit a field it behaves as expected, subsequently you get a 'Function Expected' error. The relevant code is: var valStore; function storeValue(field) { valStore = field.value; } function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; } function leavingField(field) { if (hasValueChanged(field.value)) { var prefix = (ctlPrefix + curr); calculateSingleDifference(field.id.split(prefix)[1]); field.className = (field.className + ' changedValue'); } } The error is raised the second time 'hasValueChanged' is called in 'leavingField'. Any ideas gratefully received! Thanks in advance Ben
-
Hi Hope someone can help with this as I'm feeling a bit stupid at the moment! I've got a form (using a master page and an update panel in case it's relevant). This is also (and unfortunately needs to) running in IE6. On there are 3 text boxes which are pre-populated server side. When the value of one of these changes, I need to update another part of the screen (client-side, this doesn't need to go back to the server). So, each of those text boxes has:onFocus="storeValue(this)" onBlur="leavingField(this) so that on entry to the field the value is stored in a global variable and on exit this is checked and actioned if it has changed. The first time you enter and exit a field it behaves as expected, subsequently you get a 'Function Expected' error. The relevant code is: var valStore; function storeValue(field) { valStore = field.value; } function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; } function leavingField(field) { if (hasValueChanged(field.value)) { var prefix = (ctlPrefix + curr); calculateSingleDifference(field.id.split(prefix)[1]); field.className = (field.className + ' changedValue'); } } The error is raised the second time 'hasValueChanged' is called in 'leavingField'. Any ideas gratefully received! Thanks in advance Ben
Bjohnson33 wrote:
function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; }
well, to start with you got
hasValueChanged
both as function name and variable name. I can see where that originrated :doh: try this for a size,function hasValueChanged(val) { if (val != valStore) return true; else return false; }
Yusuf May I help you?
-
Bjohnson33 wrote:
function hasValueChanged(val) { if (val != valStore) hasValueChanged = true; else hasValueChanged = false; }
well, to start with you got
hasValueChanged
both as function name and variable name. I can see where that originrated :doh: try this for a size,function hasValueChanged(val) { if (val != valStore) return true; else return false; }
Yusuf May I help you?
Thanks Yusuf, exactly right - was obviously having a syntax meltdown - fifth language this week! Ben