I want to change Image src using jquery in magento2,

So how can I get Imageurl in jQuery?

    <field name="default_template">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Ktpl\BannerManagement\Model\Config\Source\Template</item>
                    <item name="config" xsi:type="array">
                        <item name="label" xsi:type="string">Demo template</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">default_template</item>
                        <item name="component" xsi:type="string">Ktpl_BannerManagement/js/form/element/defaultTemplate</item>
    <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>


    <img alt="demo"  class="article_image" id="mp-demo-image">
    <div class="admin__field field field-load_template " data-ui-id="ktpl-bannerslider-banner-edit-tab-banner-fieldset-element-form-field-load-template" style="">
                        <label class="label admin__field-label" for="banner_load_template" data-ui-id="ktpl-bannerslider-banner-edit-tab-banner-fieldset-element-note-banner-label"><span></span></label>
                <div class="admin__field-control control">
                    <div id="banner_load_template" class="control-value admin__field-value" style=""><button id="id_lq27ZKqKzkY3i4p7JLoi57aNy7f2PDSk" title="Load Template" type="button" class="action-default scalable" data-ui-id="widget-button-0">
                    <span>Load Template</span>

if I will update default_template value I want to change default_image src using jQuery

  • 2
    Do you need it in .phtml file or seperate JS file ? – Aasim Goriya Jun 14 '19 at 10:48
  • In custom js file @AasimGoriya – Rutvee Sojitra Jun 14 '19 at 11:05
  • Please check my ans. – Aasim Goriya Jun 14 '19 at 11:13
  • but i want it in admin ui form – Rutvee Sojitra Jun 14 '19 at 11:18
  • do you have .phtml file for your admin form page ? – Aasim Goriya Jun 14 '19 at 11:19

You can add JS following way in your .phtml file

<script type="text/x-magento-init">
    "*": {
        "Vendor_Module/js/custom-js": {
            "imageURL": "<?= $imageUrl; ?>"

Now in your custom JS file you can get your Image URL using following way.
So in Vendor_Module/web/js/custom-js.js file your JS code look like below.

    ], function($){
        "use strict";

        function customFunction(config, element) {
            var imgURL = config.imageURL; //config.imageURL is already defined in your .phtml file
        return customFunction; 

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.