Opened 5 years ago

Closed 5 years ago

#14364 closed (invalid)

Admin add form, element focus script not working because of wrong id

Reported by: woutervandergraaf Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Keywords: add_form
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I get a JavaScript error for this line in the change_form.html template:

{% if adminform and add %}
   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
{% endif %}

That's because the first field in the form is the default date/time widget, consisting of 2 input fields. The referred id is not used in this widget.

Quick and dirty fix: check if element with id exists:

{% if adminform and add %}
   <script type="text/javascript">var focusEl = document.getElementById("{{ adminform.first_field.auto_id }}"); if (focusEl && focusEl.focus) { focusEl.focus(); }</script>
{% endif %}

Change History (1)

comment:1 Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Please include a minimal example model/admin definition needed to recreate this. I'm not seeing the error in a brief attempt to recreate; it would really help if I didn't have to guess what the model and admin definition for it needs to look like to reproduce the error.

Note: See TracTickets for help on using tickets.
Back to Top