JavaScript: Prototype event listener failing to trigger

I have a Magento test store with an onepagecheckout extension that utilizes a onepagecheckout.js file. I am trying to add 'click' event observers to the payment radio buttons, but nothing happens when they are clicked.

The observers are being added to individual input elements using a javascript function:


addObservers: function () {
  $$('input[name="payment[method]"]').each(function (el) {
    el.observe('click', function () {
      checkout.update({
        'review': 1,
        'payment-changed': 1
      });
    });
  });
}

The elements being looped through in the code match the ids and names of the input elements:


el = input#p_method_bankpayment.radio
el = input#p_method_paypal_express.radio
el = input#p_method_cashondelivery.radio
el = input#p_method_phoenix_cashondelivery.radio

The update function is supposed to fetch new content via AJAX when the page is loaded, but it doesn't seem to be executed as there is no network activity visible when the events should be triggered.

You can view the installation here: (Add items to cart, proceed to checkout, but stop before finalizing)

I'm wondering why the observers are not functioning properly. Other installations with this extension are working fine. Any insights?

Answer №1

Your input elements are currently hidden from view with the following style attribute:

style="display:none;"

This means that users cannot click on them. If you remove the display:none property using developer tools and then try to click the radio button, an alert saying 'Click' will appear.

Instead of relying on the click event, consider using a change event like this:

addObservers: function () {
   $$('input[name="payment[method]"]').each(function (el) {
    el.observe('change', function () {
      checkout.update({
        'review': 1,
        'payment-changed': 1
      });
    });
  });
}

Update: Upon further investigation, it seems that a change event may not be sufficient in this case because there are onclick attributes attached to each radiobutton. These onclick attributes do not trigger the onchange event. To work around this issue, you can manually trigger the onclick event using something like this:

onclick="payment.switchMethod('phoenix_cashondelivery')"

You can simulate this event by adding the following code:

$$('input[name="payment[method]"]').each(function (el) {
  el.observe('change', function () {
    $(this).simulate('click');
    checkout.update({
      'review': 1,
      'payment-changed': 1
    });
  });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality of a JQuery post within a PHP loop is not functioning optimally

I am encountering an issue with my list.php file, which generates a list with items and a button (Erledigt!) that triggers a jQuery Post to wishdelete2.php. The problem is that the jQuery post sometimes requires multiple clicks (3x or 5x) before it process ...

Tips for getting a plugin to function properly when the page is refreshed in Nuxt app

I am currently incorporating the vue GAPI plugin into my project. While it functions smoothly when navigating between pages, I encounter an error upon refreshing: vue-gapi.common.js?15fd:241 Uncaught (in promise) Error: gapi not initialized at GoogleAuth ...

Pop-Up Buttons Generated in Real-Time

As a backend developer, I often find myself struggling to understand how web browsers work when working on frontend tasks. In this case, I have left the JavaScript alert for ease of use. Depending on which button is clicked in the popover, it should displa ...

Ways to add the class "active" to the initial element in a Vue.js for loop

I am currently diving into learning vue.js and am working on adding a list of elements. My goal is to apply the class="active" only to the first element in the for loop. Below is the snippet of my code: <div class="carousel-inner text-center " role="li ...

Split panel vertically with a draggable divider allowing you to customize the height of each section

Recently diving into Angular, I'm working on a vertically split panel with a "drag handle" that allows users to adjust the height of the top and bottom tree panels. While I've managed to implement a three-stage toggle, I'm struggling to get ...

Angular push introduces a new operator that leads to a MongoError being triggered

I'm currently following along with a tutorial on the mean stack, which can be found here One of the features I am working on is implementing a delete function to remove items from the database when a button is clicked. In my client side controller, ...

What could be the reason for Jest flagging CSS as untested instead of identifying untested functions?

While working on my vue3 project and writing tests with jest, I have encountered an issue where jest is incorrectly marking the CSS within several single file components as untested, even though it doesn't need to be tested. Moreover, its assessment ...

Display a dropdown menu when the value of another dropdown is changed using .NET 2.0

I have a dropdown selection that I would like to trigger another dropdown to display based on the selected index change. How can I achieve this using the onchange function in JavaScript? I am working on an older project using .NET 2.0, so I am unable to ...

Is it possible to load the response from the $.post function, which contains GIF content, into a JavaScript Image object

How can I preload an image that the server only returns on a POST request? I want to load the image before displaying it. How can I store the result in a JavaScript object? $.post('image.php', {params: complexParamsObject}, function(result) { ...

Binding data to an MVC grid component

I am working with asp.net mvc3.0 razor framework and using an html Table to populate data from ViewBag based on the selected option in a dropdownlist. public ActionResult Index(GetFileName fn) { .... ViewBag.Grid1Data = dt; return View(); } Initially, th ...

Transfer a JSON object to a Java Class without relying on a servlet

I have a form in HTML where I collect user input and store it as an object in JavaScript. Here is how I am creating the object: var dataObject = { Name: getName(), Age : getAge() } Now, I want to send this object using Ajax to a b ...

Utilizing the fetch() method in Vuex to obtain a restful API

Struggling to integrate my API data through Vuex, I am in dire need of a reliable guide or perhaps someone who can assist me with this task. Previously, without using Vuex, all my requests functioned flawlessly. However, now I'm unsure about the neces ...

Achieve a new line break when the user hits the "Enter" key using HTML and JavaScript

I'm in the process of developing a Chrome Extension that allows users to create a to-do list. My goal is to enable users to submit their task by pressing the "Enter" key, which should move the task to the next line after submission. I am currently fac ...

Selenium error: Element is unclickable ... Another element is blocking the click, but go ahead and click

When attempting to utilize the code snippet below return this.driver.findElement(By.css("div[class*='img']")).click(); An error occurs: Uncaught WebDriverError: unknown error: Element is not clickable at point (525, 889). Other element would r ...

Combining multiple events into one function using jQuery

I am searching for the opposite of what everyone else is seeking. I have an anonymous jQuery function that I want to keep as it is, but I need to attach multiple event handlers to it on different occasions (specifically two events). When the text inside ...

js specific to a button

I have multiple buttons on my page, but I want a script to only run for one specific button. <script> $(document).ready(function(){ if (window.localStorage.getItem('accept_cookies')) { $("button").parent().paren ...

Show a popover within a parent div that has limited visible space due to its hidden overflow

Struggling with AngularJS, I can't seem to find a simple solution for this problem. In my code, there's a div element with the overflow: hidden property set due to an internal scrollbar. Inside this div, there's a dropdown menu that is trigg ...

A guide on dynamically filling a dropdown menu based on the selection made in another dropdown using PHP and JSON data

Looking to create a dynamic select box based on another select box <select> <option value="x">x</option> <option value="y">y</option> </select> <select> <option value="1">1</option> ...

Create a custom loading spinner for a jQuery AJAX request

How can I add a loading indicator to my Bootstrap modal that is launched from a link? Currently, there is a 3-second delay while the AJAX query fetches data from the database. Does Twitter Bootstrap have built-in functionality for this? UPDATE: Modified J ...

Capture XHR requests and alter the response body

I'm in the process of developing a script that serves as a proxy/wrapper for the conventional XMLHttpRequest object. This will allow me to intercept it, alter the responseText, and then return it back to the original onreadystatechange event. The mai ...