0

For example, I have a simple admin UI form with one text input field:

<field name="my_text" formElement="input">
    <settings>
        <dataType>text</dataType>
        <label>My Text</label>
    </settings>
</field>

How can I add ID attribute to it?

  • you may want to elaborate your question a bit more? the form you are showing here has not javascript whatsoever and I don't see what you are trying to do to be honest? – Herve Tribouilloy Jul 21 '18 at 13:03
1

The main idea here, that Knockout.js generates ID dynamically.
Generated ID is available in a KO template, or in corresponding UI component's javascript file as uid variable.

In order to customize form element component in admin you can use your own template or ui component that extends Magento_Ui/js/form/element/abstract class.

For example, we have a text input component. And we need to use element ID in some other attribute.
At first, you should define custom template for a component:

<field name="my_text" formElement="input" template="template="Your_Module/form/custom-input">
    <settings>
        <dataType>text</dataType>
        <label>My Text</label>
    </settings>
</field>

Then in a template it's possible to access element ID attribute in data-bind attribute:

<button data-bind="attr: {'custom-attribute': 'dynamic_attribute_example' + uid}"></button>

In this example we set dynamic attribute custom-attribute for the button.

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.