0

I have created a form using UI component, I want to retrieve value selected select box option on change. I have created a requirejs-config.js file

var config = {
map: {
    '*': {
        customAdmin: 'Namespace_Modulename/js/customAdmin'
    }
},
deps: ["jquery"]
};

A custom js file

require(["jquery"], function ($) {
//your js code here
$(document).ready(function () {
alert('js loaded');
    $('select[name="custom_attr_name"]').change(function () {
        alert($(this).val());
    });
   });
});

I am getting an alert but not getting any value for the change of select box. When I run the same code on the console, everything works fine. I don't know whats wrong going on in this? Please help me in identifying the issue.

1
  • Answer of @dev_67Commerce working for you? Oct 9 '18 at 6:15
2

Magento2 JS loads after the body is loaded. So whenever the page is loaded, the scripts are loaded simultaneously. You can try something like:-

require(['jquery'],
    function ($) {
        $(document).ready(function() {
            setTimeout(function(){
                  $('select[name="custom_attr_name"]').change(function () {
                      alert($(this).val());
                  });
              },5000);
    });
})

Or if it does not works try using body load inside the ready() function.

7
  • Its not working
    – user00247
    Nov 21 '17 at 7:25
  • Can you check if the jquery is working for you in the module, not via console but in the module. Can you try placing the snippet in the phtml file first prior to adding it in custom js file, just to confirm that jquery is actually working Nov 21 '17 at 7:27
  • Its not working even in phtml file
    – user00247
    Nov 21 '17 at 8:22
  • Do you mean jquery is not working in .phtml file? Or just the functionality you are trying to do? Nov 21 '17 at 9:04
  • Functionality which I am trying to do is not working in phtml also.
    – user00247
    Nov 21 '17 at 10:51
1

Add domready for that

require(["jquery","domReady!"], function ($) {
    $('select[name="custom_attr_name"]').change(function () {
        alert($(this).val());
   });
});
6
  • I had tried it but it doesn't worked for me.
    – user00247
    Nov 21 '17 at 7:24
  • jQuery('select[name="custom_attr_name"]').length check in console, is that provide any length Nov 21 '17 at 7:42
  • Not getting anything in console on change.
    – user00247
    Nov 21 '17 at 8:22
  • i said to check in crome browser console, are you gettiing any length Nov 21 '17 at 8:28
  • Yes, I checked in crome browser console.
    – user00247
    Nov 21 '17 at 10:50

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.