Opened 15 years ago
Closed 15 years ago
#14364 closed (invalid)
Admin add form, element focus script not working because of wrong id
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 %}
Note:
See TracTickets
for help on using tickets.
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.