I have created custom model and backend for it (grid and create/edit form). Now I need to add ajax autocomplete to one of the input fields of this form. How can I add custom javascript to edit/create form?

Any examples or links to examples would be appreciated.

  • Did you create grid and form using ui components
    – Jaisa
    Mar 13, 2018 at 3:21

2 Answers 2


Using getAfterElementHtml() function,you can add custom JavaScript at your form.

Suppose,you have $form ( @var \Magento\Framework\Data\Form $form )

   $form = $this->_formFactory->create();
/* you need this below code */
    $Lastfield = $form->getElement('Your_last_field_Name');
                '<script> add your JavaScript code
  • Hi @AmitBera tried doing as above then getting this responce "Changes have been made to this section that have not been saved. This tab contains invalid data. Please resolve this before saving.", hope you can help. I am calling my last input field name of admin form.. May 25, 2016 at 13:41

Magento 2 has setAfterElementJs() for custom JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.