forms: use standalone js to give focus to the first field with errors (#11137)

This commit is contained in:
Frédéric Péters 2016-05-30 20:21:30 +02:00
parent 1717eebe01
commit 1f93ef96f4
2 changed files with 6 additions and 17 deletions

View File

@ -361,23 +361,6 @@ class Form(QuixoteForm):
if self.captcha:
r += self.captcha.render()
r += self._render_submit_widgets()
if self.has_errors():
# if form has errors, focus and select the first widget on error
r += htmltext("""<script type="text/javascript">
var all_obj = document.getElementsByTagName('DIV');
for (i=0; i<all_obj.length; i++) {
if ( ( ' ' + all_obj[i].getAttribute("class") + ' ').toLowerCase().match(
new RegExp ( '^.* widget-with-error .*$', 'g' ) ) ) {
less_obj = all_obj[i].getElementsByTagName('INPUT');
for (j=0; j<less_obj.length; j++) {
less_obj[j].select();
less_obj[j].focus();
break;
}
break;
}
}
</script>""")
return r.getvalue()
def _render_prefill_widgets(self):

View File

@ -31,4 +31,10 @@ $(function() {
$('#tracking-code button').click(function() {
$('input[name=savedraft]').click();
});
if ($('.widget-with-error').length) {
var first_field_with_error = $($('.widget-with-error')[0]).find('input,textarea,select');
if (first_field_with_error.length) {
$(first_field_with_error)[0].focus();
}
}
});