Magento 2 how to add a Javascript addeventlistener to element The Next CEO of Stack OverflowIf Maximum order value is above £X show Javascript AlertInjecting javascript in specific pageMagento 2: Adding external CSS and JS to only one content page, and use jquery functions inside the contentMagento 2: Add UI Multiselect DropdownHow do you populate a Magento_UI dynamicRows element?when proceed to checkout take very long and getting proxy errorLoading Javascript for minicart in Magento 2add custom js to the certain input element on the checkout pageMagento Checkout Stuck: Failed to load the “Magento_Checkout/js/view/payment” componentSOLVED - Add js to home page

When did Lisp start using symbols for arithmetic?

Unreliable Magic - Is it worth it?

Should I tutor a student who I know has cheated on their homework?

How to count occurrences of text in a file?

Why does GHC infer a monomorphic type here, even with MonomorphismRestriction disabled?

Inappropriate reference requests from Journal reviewers

Can the Reverse Gravity spell affect the Meteor Swarm spell?

Visit to the USA with ESTA approved before trip to Iran

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Explicit solution of a Hamiltonian system

% symbol leads to superlong (forever?) compilations

How to be diplomatic in refusing to write code that breaches the privacy of our users

Customer Requests (Sometimes) Drive Me Bonkers!

Grabbing quick drinks

How to write the block matrix in LaTex?

Implement the Thanos sorting algorithm

Can a caster that cast Polymorph on themselves stop concentrating at any point even if their Int is low?

What size rim is OK?

Describing a person. What needs to be mentioned?

Whats the best way to handle refactoring a big file?

The King's new dress

Term for the "extreme-extension" version of a straw man fallacy?

What does this shorthand mean?

Is there a good way to store credentials outside of a password manager?



Magento 2 how to add a Javascript addeventlistener to element



The Next CEO of Stack OverflowIf Maximum order value is above £X show Javascript AlertInjecting javascript in specific pageMagento 2: Adding external CSS and JS to only one content page, and use jquery functions inside the contentMagento 2: Add UI Multiselect DropdownHow do you populate a Magento_UI dynamicRows element?when proceed to checkout take very long and getting proxy errorLoading Javascript for minicart in Magento 2add custom js to the certain input element on the checkout pageMagento Checkout Stuck: Failed to load the “Magento_Checkout/js/view/payment” componentSOLVED - Add js to home page










0















I am trying to add an event listener to an element, but no matter what I try, it won't work. Here is what I have so far (this is in the html web templates page):



window.addEventListener('load',function()
document.getElementById('authnetcim-submit').addEventListener("click", function()
alert('Alert Message');
);
);


Everyone keeps saying that the listener needs to be wrapped in window.addEventListener('load'), as you need to wait for the page to load before you can add the listeners, but that still won't work for me either. There are no errors and nothing logs to the console. The Id is correct and it's the only id of that name used on the page.



Any help is appreciated.










share|improve this question


























    0















    I am trying to add an event listener to an element, but no matter what I try, it won't work. Here is what I have so far (this is in the html web templates page):



    window.addEventListener('load',function()
    document.getElementById('authnetcim-submit').addEventListener("click", function()
    alert('Alert Message');
    );
    );


    Everyone keeps saying that the listener needs to be wrapped in window.addEventListener('load'), as you need to wait for the page to load before you can add the listeners, but that still won't work for me either. There are no errors and nothing logs to the console. The Id is correct and it's the only id of that name used on the page.



    Any help is appreciated.










    share|improve this question
























      0












      0








      0








      I am trying to add an event listener to an element, but no matter what I try, it won't work. Here is what I have so far (this is in the html web templates page):



      window.addEventListener('load',function()
      document.getElementById('authnetcim-submit').addEventListener("click", function()
      alert('Alert Message');
      );
      );


      Everyone keeps saying that the listener needs to be wrapped in window.addEventListener('load'), as you need to wait for the page to load before you can add the listeners, but that still won't work for me either. There are no errors and nothing logs to the console. The Id is correct and it's the only id of that name used on the page.



      Any help is appreciated.










      share|improve this question














      I am trying to add an event listener to an element, but no matter what I try, it won't work. Here is what I have so far (this is in the html web templates page):



      window.addEventListener('load',function()
      document.getElementById('authnetcim-submit').addEventListener("click", function()
      alert('Alert Message');
      );
      );


      Everyone keeps saying that the listener needs to be wrapped in window.addEventListener('load'), as you need to wait for the page to load before you can add the listeners, but that still won't work for me either. There are no errors and nothing logs to the console. The Id is correct and it's the only id of that name used on the page.



      Any help is appreciated.







      magento2 magento2.2 javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Eric BrownEric Brown

      1456




      1456




















          1 Answer
          1






          active

          oldest

          votes


















          0














          You only need the domReady! dependency. If you're in Magento 2, you should be using require JS anyways. Here's the best of both worlds.



          define([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );


          OR require instead of define. Depends on how you're calling the functionality.



          require([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );





          share|improve this answer























          • I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

            – Eric Brown
            yesterday












          • console.log(window) to see if window is null. If it is you have a different issue

            – Shawn Abramson
            yesterday











          • It's not null, it prints Window object.

            – Eric Brown
            23 hours ago











          • how does the authnetcim-submit element get output to the page?

            – Shawn Abramson
            23 hours ago











          • I believe it's through Knockout.

            – Eric Brown
            6 hours ago











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f267529%2fmagento-2-how-to-add-a-javascript-addeventlistener-to-element%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You only need the domReady! dependency. If you're in Magento 2, you should be using require JS anyways. Here's the best of both worlds.



          define([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );


          OR require instead of define. Depends on how you're calling the functionality.



          require([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );





          share|improve this answer























          • I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

            – Eric Brown
            yesterday












          • console.log(window) to see if window is null. If it is you have a different issue

            – Shawn Abramson
            yesterday











          • It's not null, it prints Window object.

            – Eric Brown
            23 hours ago











          • how does the authnetcim-submit element get output to the page?

            – Shawn Abramson
            23 hours ago











          • I believe it's through Knockout.

            – Eric Brown
            6 hours ago















          0














          You only need the domReady! dependency. If you're in Magento 2, you should be using require JS anyways. Here's the best of both worlds.



          define([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );


          OR require instead of define. Depends on how you're calling the functionality.



          require([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );





          share|improve this answer























          • I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

            – Eric Brown
            yesterday












          • console.log(window) to see if window is null. If it is you have a different issue

            – Shawn Abramson
            yesterday











          • It's not null, it prints Window object.

            – Eric Brown
            23 hours ago











          • how does the authnetcim-submit element get output to the page?

            – Shawn Abramson
            23 hours ago











          • I believe it's through Knockout.

            – Eric Brown
            6 hours ago













          0












          0








          0







          You only need the domReady! dependency. If you're in Magento 2, you should be using require JS anyways. Here's the best of both worlds.



          define([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );


          OR require instead of define. Depends on how you're calling the functionality.



          require([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );





          share|improve this answer













          You only need the domReady! dependency. If you're in Magento 2, you should be using require JS anyways. Here's the best of both worlds.



          define([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );


          OR require instead of define. Depends on how you're calling the functionality.



          require([
          'domReady!'
          ], function ()
          'use strict';

          //do your thing here
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Shawn AbramsonShawn Abramson

          2,4871915




          2,4871915












          • I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

            – Eric Brown
            yesterday












          • console.log(window) to see if window is null. If it is you have a different issue

            – Shawn Abramson
            yesterday











          • It's not null, it prints Window object.

            – Eric Brown
            23 hours ago











          • how does the authnetcim-submit element get output to the page?

            – Shawn Abramson
            23 hours ago











          • I believe it's through Knockout.

            – Eric Brown
            6 hours ago

















          • I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

            – Eric Brown
            yesterday












          • console.log(window) to see if window is null. If it is you have a different issue

            – Shawn Abramson
            yesterday











          • It's not null, it prints Window object.

            – Eric Brown
            23 hours ago











          • how does the authnetcim-submit element get output to the page?

            – Shawn Abramson
            23 hours ago











          • I believe it's through Knockout.

            – Eric Brown
            6 hours ago
















          I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

          – Eric Brown
          yesterday






          I tried this out, but it still wasn't working. I'm getting the error "Cannot read property 'addEventListener' of null." I added in a .phtml file require([ 'domReady!' ], function () alert('phtml loaded'); var getElementById = document.getElementById("billing-address-same-as-shipping-authnetcim"); document.getElementById('authnetcim-submit').addEventListener("click", function() alert('Alert Message'); ); );

          – Eric Brown
          yesterday














          console.log(window) to see if window is null. If it is you have a different issue

          – Shawn Abramson
          yesterday





          console.log(window) to see if window is null. If it is you have a different issue

          – Shawn Abramson
          yesterday













          It's not null, it prints Window object.

          – Eric Brown
          23 hours ago





          It's not null, it prints Window object.

          – Eric Brown
          23 hours ago













          how does the authnetcim-submit element get output to the page?

          – Shawn Abramson
          23 hours ago





          how does the authnetcim-submit element get output to the page?

          – Shawn Abramson
          23 hours ago













          I believe it's through Knockout.

          – Eric Brown
          6 hours ago





          I believe it's through Knockout.

          – Eric Brown
          6 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f267529%2fmagento-2-how-to-add-a-javascript-addeventlistener-to-element%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

          Magento2 - How to hide price filter only in specific categories?Multiselect price filter attribute in layered navigationhide only some categories from layered navigation in magentoRemove Price Filter on certain categoriescustomize layered price filter?Hide Price for a particular customer groupPrice filter in layered navigation not working correctly with price including tax in magento 2.2.3Magento 2 how to hide attribute at Layered navigation?Magento 2. how to hide price only for specific categoriesMagento 2 How can I hide the price and total from cart and checkout summary?Magento2: Can we add navigation layered filter like price filter for other attribute?