1

I successfully added a JS file to my page and every console.log is working okay. Unfortunately no other function is working.

This is how my file looks like.

require([
    "jquery",
    "jquery/ui",
    "domReady!"
], function($){

  $(document).ready(function() {

    console.log('does it work?');

    if(1 == 1) {
      console.log("true");
    } else {
      console.log("false");
    };

  });

});

And it doesn't work as well - no output. Does anybody has an idea what could be the problem?

2
  • 1
    RequireJs doesn't execute until after the DOM is loaded, so you need to remove the ready event. Commented Dec 16, 2016 at 21:33
  • But how is it possible that the first console log is working? Commented Dec 16, 2016 at 21:46

1 Answer 1

2

Your code lines should work. However, make sure that this file is in static content also.
If we change only on our module, our function may not work. Magento will load js files from pub/static content folder, not from your module (under app folder).

There is a way to debug and check our js: we can check the Sources in Browser. Make sure your new change is in this file. We can disable Browser Cache for testing purpose.

enter image description here

From my experience, we can edit the js file directly in static content folder. If our function can work, we will copy it to our "real" js file in module or theme.

6
  • 1
    You can also add the debugger; keyword to your code. When you then surf in chrome or firefox with the developper tools open the execution of the javascript wil stop at that breakpoint. More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger Commented Dec 17, 2016 at 19:30
  • @AndrejZemplényi you're right, your code lines should work. Commented Dec 19, 2016 at 13:01
  • So this actually helped me to find the root of the problem. My JS actually works but it seems like just a few of jQuery functions or any JS applies. For example .hide() doesn't work on any element but changing it by .html() it produces some results (But only on aside element)... However my point is to select an input and do some stuff with it. But selecting an input by $("input[name='company']"); doesn't work neither does work selecting parent div or mostly any other div. What could be the case? Is it beceuse of the AJAX that is loading that it doesn't find it? Thanks for help Commented Dec 19, 2016 at 13:03
  • @AndrejZemplényi you should open new question. I will take a look at the new issue. Commented Dec 19, 2016 at 13:05
  • magento.stackexchange.com/questions/150957/… here's the link. Thanks. Commented Dec 19, 2016 at 13:44

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.