Skip to content

Instantly share code, notes, and snippets.

@devbas
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbas/734729ee9fee7b53fe5c to your computer and use it in GitHub Desktop.
Save devbas/734729ee9fee7b53fe5c to your computer and use it in GitHub Desktop.
Custom action for required attribute in HTML5 Forms
document.addEventListener("DOMContentLoaded", function() {
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++) {
elements[i].oninvalid = function(e) {
var start = new Date().getTime();
for(var i = 0; i < e.path.length; i++) {
if(e.path[i]['classList']) {
var parentClass = jQuery.inArray('postbox-container', e.path[i]['classList']);
if(parentClass != -1) {
if(e.path[i]['classList'][parentClass]) {
var container = jQuery('#postbox-container-2').children().children();
var itemId = e.path[i]['previousElementSibling'];
console.log('item id: ', itemId);
jQuery(container).css('display', 'none');
//jQuery()
}
}
}
}
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ', time);
//console.log('parent class: ', e.path);
var parentClass = jQuery.inArray('document', e.path);
//console.log('parent class: ', parentClass);
e.target.setCustomValidity("");
if (!e.target.validity.valid) {
e.target.setCustomValidity("This field cannot be left blank");
}
};
elements[i].oninput = function(e) {
e.target.setCustomValidity("");
};
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment