Opened 4 years ago

Closed 4 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:


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 %}

Attachments (0)

Change History (1)

comment:1 Changed 4 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.