Skip to content

Commit

Permalink
Merge pull request #47 from jrpence/feature/20200728-NullElementIssue
Browse files Browse the repository at this point in the history
Null element issue
  • Loading branch information
ChemiKyle authored Aug 3, 2020
2 parents 80a3f8b + 956b2d4 commit c7bb284
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions js/default_when_visible.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
autoPopulateFields.defaultWhenVisible.init = function() {

// Extracting evalLogic function body.
var evalLogicBody = evalLogic.toString();
var evalLogicBody = evalLogicBody.slice(evalLogicBody.indexOf('{') + 1, evalLogicBody.lastIndexOf('}'));

var evalLogicBodyString = evalLogic.toString();

// To prevent style setting on nonexistent elements adding in a check for existence before trying to hide an element.
evalLogicBodyString = evalLogicBodyString.replace(
/document\.getElementById\(this_field\+\'\-tr\'\)\.style\.display=\'none\';/g,
'if ( document.getElementById(this_field + \'-tr\') != null ){ document.getElementById(this_field + \'-tr\').style.display = \'none\'; }'
);

evalLogicBody = evalLogicBodyString.slice(evalLogicBodyString.indexOf('{') + 1, evalLogicBodyString.lastIndexOf('}'));

// Changing evalLogic() function behavior: hide fields even when the message is not shown.
var target = 'var eraseIt = false;';
var replacement = 'var eraseIt = false; document.getElementById(this_field + \'-tr\').style.display = \'none\';';
var replacement = 'var eraseIt = false; if( document.getElementById(this_field + \'-tr\') != null ){ document.getElementById(this_field + \'-tr\').style.display = \'none\'; }';

// Overriding original function.
evalLogic = new Function('this_field', 'byPassEraseFieldPrompt', 'logic', evalLogicBody.replace(target, replacement));
Expand All @@ -19,8 +27,13 @@ autoPopulateFields.defaultWhenVisible.init = function() {
formSubmitDataEntry = function() {
$.each(autoPopulateFields.defaultWhenVisible.branchingEquations, function(fieldName, equation) {
// If equation result is false, erase field value.
if (!eval(equation)) {
evalLogicSubmit(fieldName, false, false);
try {
if ( !eval( equation ) ) {
evalLogicSubmit( fieldName, false, false );
}
}
catch ( e ) {
evalLogicSubmit( fieldName, false, false );
}
});

Expand Down

0 comments on commit c7bb284

Please sign in to comment.