1

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.

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

2 Answers 2

4

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');
       $Lastfield->setAfterElementHtml(
                '<script> add your JavaScript code
                </script>'
        );
1
  • 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.. Commented May 25, 2016 at 13:41
1

Magento 2 has setAfterElementJs() for custom JavaScript.

Your Answer

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

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