Created
January 29, 2018 17:09
-
-
Save guillaumepiot/e1a1a7d7075625f9d9c0683f96a7502e to your computer and use it in GitHub Desktop.
Show / hide fields based on field value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var required_financial_fields = {{LOAN_REQUIRED_FINANCIAL_FIELDS|safe}}; | |
var fields = [] | |
for(var key in required_financial_fields) { | |
fields = fields.concat(required_financial_fields[key]) | |
} | |
function hideAllFields() { | |
for(var i = 0; i < fields.length; i++) { | |
document.querySelector('[name='+fields[i]+']').parentNode.parentNode.style.display = "none"; | |
} | |
} | |
function showFields(key) { | |
hideAllFields() | |
for(var i = 0; i < required_financial_fields[key].length; i++) { | |
document.querySelector('[name='+required_financial_fields[key][i]+']').parentNode.parentNode.style.display = "block"; | |
} | |
} | |
var category = document.getElementById('id_category'); | |
showFields(category.value) | |
category.addEventListener('change', function() { | |
showFields(this.value) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment