Skip to content

Instantly share code, notes, and snippets.

@jdavidbakr
Last active August 29, 2015 14:21
Show Gist options
  • Save jdavidbakr/7b71986312de98af7cc4 to your computer and use it in GitHub Desktop.
Save jdavidbakr/7b71986312de98af7cc4 to your computer and use it in GitHub Desktop.
JavaScript: update_subform
var HiddenSubform = {
update: function(select) {
var selectedIndex = select.options.selectedIndex;
for (var i = 0; i < select.options.length; i++) {
var sel = select.options[i].getAttribute('show');
if (sel) {
var dom = document.querySelector(sel);
if (dom) {
if (i == selectedIndex) {
removeClass(dom, 'hidden');
} else {
addClass(dom, 'hidden');
}
}
}
}
var url = select.getAttribute('url');
var form = this.getForm(select);
if(form && url) {
// Serialize using the AjaxForm serializer
var data = AjaxForm.serialize(form);
microAjax(url, this.callback_response.bind(this), data);
}
},
getForm: function(node) {
/**
* Traverse up the tree until we find the parent form
* @param {dom} !node The node we are wanting to find the parent of
* @return {dom} The form element, or null if none found
*/
if(!node) {
return null;
}
var par = node.parentNode;
if(par && par.nodeName == 'FORM') {
return par;
}
return this.getForm(par);
},
callback_response: function(json) {
process_json(json);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment