I have the following table. In this table I have dynamically added rows of the following form:
<tr class="conditionalRow">
<td class="logicalData">
<select oldvalue="AND" class="logicSelectionMenu">
<option value="AND">AND</option>
<option value="AND (">AND (</option>
<option value="OR">OR </option>
<option value="OR (">OR (</option>
<option value=")">)</option>
</select>
</td>
<td class="fieldData">
<p>Other Data that you don't need to worry about</p>
</td>
<td class="conditionData">
<p>Other Data that you don't need to worry about</p>
</td>
<td class="compareData">
<p>Other Data that you don't need to worry about</p>
</td>
<td>
<button class="removeConditionButton" type="button"> - </button>
</td>
</tr>
I use the following jQuery selectors to handle the onchange events:
$(document).ready(function() {
$(".logicSelectionMenu").change(function() {
var row = $(this).closest("tr");
updateLogicMenu(row);
});
$(".logicSelectionMenu").focus(function() {
$(this).attr("oldValue",$(this).val());
});
});
function updateLogicMenu(inRow) {
var selectedVal = $(inRow).find(".logicSelectionMenu").attr("value");
var oldVal = $(inRow).find(".logicSelectionMenu").attr("oldValue");
/* -=> VERY IMPORTANT LINE BELOW!!! <=- */
// alert("Hi, I cause a time delay");
if (selectedVal == ")") {
// clears cell contents if ")" is choosen by user
$(inRow).find(".fieldSelectionMenu" ).css("visibility","hidden").html("");
$(inRow).find(".conditionSelectionMenu").css("visibility","hidden").html("");
$(inRow).find(".compareData" ).css("visibility","hidden").html("");
}
else if(oldVal == ")" || oldVal === undefined) {
// regenerates cell contents when user changes selection from ")" to another
alert("regenerating");
$(inRow).find(".fieldSelectionMenu").css("visibility","visible").html(getFieldSelectionOptions());
$(inRow).find(".conditionSelectionMenu").css("visibility","visible");
$(inRow).find(".compareSelectionMenu").css("visibility","visible");
updateFieldMenu(inRow); // function regenerates the next cell contents
// and calls another function
// which regenerates the next cell contents,
// and chains on and on ... etc
}
else { ; } // no action is needed,no clearing or regeneration
}
The problem is that when I select the “)” option from the drop down menu and then select another option. the page does now behave as expected.
-
When the very important line IS NOT commented out, both alert boxes pops up saying “I cause a time delay” and “regenerating” and the contents of the following cells get regenerated as expected.
-
When the very important line IS commented out, the JavaScript does not enter the else clause and the content of the following cells are NOT regenerated.
What is causing this alert box to cause the page to behave as expected, but it’s absence make the page behave unexpectedly? Is it the time delay of the user clicking the OK button? I do not want this alert box on the production page so how do I make the page behave the same way with or without the alert box?
Thanks to ahren’s observation I have found a fix for my code.
ahren’s observation:
“The focus event fires again after selection is changed, and therefore oldVal is updated. Having the alert() removes this focus and prevents it from firing again”
The Fix:
Set the value of
oldValueat the end of the function and letoldVal === undefinedcatch the first pass through the function.