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 '18 at 3:21

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
| improve this answer | |
  • 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.. – Mohammad Mujassam May 25 '16 at 13:41

Magento 2 has setAfterElementJs() for custom JavaScript.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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