2

I want to hide upload file filed using jquery in magento2

    <field name="type">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Ktpl\BannerManagement\Model\Config\Source\Type</item>
                    <item name="config" xsi:type="array">
                        <item name="label" xsi:type="string">Type</item>
                        <item name="visible" xsi:type="boolean">true</item>
                        <item name="dataType" xsi:type="string">select</item>
                        <item name="formElement" xsi:type="string">select</item>
                        <item name="dataScope" xsi:type="string">type</item>
                        <item name="component" xsi:type="string">Ktpl_BannerManagement/js/form/element/element</item>
                    </item>
                </argument>
            </field>
      <field name="default_image">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="visible" xsi:type="boolean">true</item>
                        <item name="formElement" xsi:type="string">input</item>
                        <item name="elementTmpl" xsi:type="string">Ktpl_BannerManagement/form/element/defaultTemplate</item>
                        <item name="additionalClasses" xsi:type="string">bannerdefaultimageClass</item>
                    </item>
                </argument>
            </field>

code in js:

     var field4 = jQuery('body').find(".bannerimageClass");

        if (value == 1) {
            field4.hide();
        } else {
            field4.show();
        }

but it not working

  • accept if helpful !!! – MageCoder Jul 9 '19 at 9:18
2

Wrap your code in setInterval(), your code run before element was build

window.setInterval(function(){
  var field4 = jQuery('body').find(".bannerimageClass");

        if (value == 1) {
            field4.hide();
        } else {
            field4.show();
        }
}, 1000);
1

Your JS should be like this.

Ktpl_BannerManagement/js/form/element/element

define([
    'jquery',
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',      
    'Magento_Ui/js/modal/modal'
], function ($,_, uiRegistry, text, modal) {
    'use strict';
    return select.extend({      

        initialize: function (){
            this.showHideField();
        },
        showHideField: function(){
            selectValue = $('input[name="type"]').val();
            if(selectValue == 'yes') {
                //Do stuff with your code
            }
        }, 
    });
});

Take refrance From here

I hope it helps!

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.