In JavaScript, the radio input will be deselected when the user decides to cancel after clicking by using `onclick="return confirm('msg');"`

UPDATE:

It seems like this problem only occurs in Internet Explorer. Here is a sample code demonstrating the issue:

<html>
    <body>
        <SPAN id=selectOperacion class=x2j>
            <INPUT onclick="return confirm('testing');" type=radio name=selectOperacion id=selectOperacion:_0>
            <LABEL title=Loan for=selectOperacion:_0>Loan</LABEL>

            <INPUT onclick="return confirm('testing');" type=radio name=selectOperacion id=selectOperacion:_1>
            <LABEL title="Material Transfer" for=selectOperacion:_1>Material Transfer</LABEL></SPAN>
    </body>
</html>

Original post:

I have defined two radio buttons using Apache Trinidad as follows:

<tr:selectOneRadio id="selectOperacion" 
    value="#{myBean.operacion}" 
    autoSubmit="true" 
    valueChangeListener="#{myBean.myChangeListener}"
    onclick="return confirm('test');" >
        <f:selectItems value="#{myBean.options}" />
</tr:selectOneRadio>

This code generates two radio input elements.

The problem arises when the user clicks "Cancel" on the confirm dialog, causing both radio buttons to become unselected.

Is there a way to maintain the selected state of the radio button even if confirm() returns false?

Thank you in advance.

Answer №1

It appears to be a bug specific to Internet Explorer.

To resolve the issue, I implemented a solution by storing the previous state of the radio buttons in variables during the onmousedown event. Subsequently, in the onclick event, a function is invoked to display a confirmation dialog. If the user chooses to cancel, the state of the radio buttons is restored.

onmousedown="update_state();"
onclick="return updateRadios(this);"

<script type="text/javascript"> 
//<![CDATA[
    var prev_radio0_state;
    var prev_radio1_state;

    // param represents the radiobutton that was clicked
    function updateRadios(param) { 
        var radio0 = document.getElementById("selectOperacion:_0");
        var radio1 = document.getElementById("selectOperacion:_1");

        if (radio0 == null || radio1 == null) {
            // no action required if only one radio button exists
            return false;
        }

        if ((param === radio0 && prev_radio0_state === true) || (param === radio1 && prev_radio1_state === true)) {
            // do nothing if a previously selected button is clicked again
            return false;
        }

        // both radio buttons exist and the unselected one was clicked

        var ok = confirm('Are you sure?');

        if (ok) {
            return true;
        }

        radio0.checked = prev_radio0_state;
        radio1.checked = prev_radio1_state;

        return false;
    }

    // Saves the checked state of the radio buttons before changing it in the click event.
    // Called in the onmousedown event
    function update_state() {
        var radio0 = document.getElementById("selectOperacion:_0");
        var radio1 = document.getElementById("selectOperacion:_1");

        prev_radio0_state = radio0 !== null ? radio0.checked : null;
        prev_radio1_state = radio1 !== null ? radio1.checked : null;
    }
//]]>
</script>

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

Nested routes are arranged in the depths and a variety of components appear depending on the path of the

I've encountered a situation where I have deeply nested routes in my routes.js file. The code snippet below shows that depending on the route, I need to render different components. For instance, if the route is for products, I should render the Produ ...

Issues with Drupal's $get function have been preventing the retrieval of complete

I've created a custom Drupal module where I'm trying to call the .module file using $get. It's functioning correctly, but it's returning the entire HTML page. I attempted to use drupal_json_output since I am working on Drupal 7. I also ...

The instance is referring to "close" as a property or method during render, but it is not defined. Ensure that this property is reactive and properly defined

Upon my initial foray into Vue.js, I encountered a perplexing warning: vue.runtime.esm.js?2b0e:619 [Vue warn]: Property or method "success" is not defined on the instance but referenced during render. Make sure that this property is reactive, e ...

Nested ng-repeat using td to display multiple items in AngularJS

I am having an issue with the code as it is producing a table with the elements all in a single column. Below is an example of the data: var data = [[{"id":"1","value":"One"},{"id":"2","value":"Two"},{"id":"3","value":"three"}],[{"id":"4","value":"four"} ...

Tips for shifting a stuck div 200px upward once the bottom of the page is reached while scrolling:

I've set up a page layout with two columns, one fixed and the other scrollable. The left column is fixed (containing Google ads) and stays in place as I scroll down the page. The right column displays posts and scrolls along with the rest of the con ...

How to set and update the value of a TextField in ReactJS using useState and implement an onchange event handler for the input field in TypeScript

Just starting out with ReactJS and MUI development, I have a form with a MuiText field in TypeScript. Seeking assistance on how to use the useState method to update the text field value. Additionally, looking for guidance on adding an onchange function to ...

Using jQuery Ajax and PHP for Secure User Authentication

Hello everyone, I could really use some assistance right now. The issue I'm facing is that the error div is not showing the content I expect in the success function of my AJAX request. I've tried alerting the response, which returns true if the ...

"Effortlessly Engage Users with Rails and AJAX Comment Posting

Running a simple blog app dedicated to video game reviews, I encountered an issue with AJAX. As a self-taught student developer, I'm facing a challenge where posting comments on review pages triggers a full page refresh instead of dynamically updating ...

How do I use jQuery to remove a dynamically added class from the page's header?

When using an inline editor to modify css classes, I encounter the need to remove and then re-add a class definition after making changes. Additionally, users have the option to delete elements, so it's important that the definition is also deleted. ...

Error encountered while invoking web server method in C# through ajax resulting in a 500 Internal Server Error

Occasionally encountering a 500 internal server error when calling a server method from an AJAX request has left me perplexed. The inconsistency of the issue, sometimes working fine and sometimes not, is baffling. To add to the confusion, no changes were m ...

Tips for accessing the content of a div tag with Protractor

Currently, I am working on developing a test that needs to click on a link within a div. However, there is an issue with my XPath locator due to some recent changes made by the developer in the UI. Using Xpath may not be a viable solution as the UI is subj ...

How to upload an image using Java and store it on a server without the use of Html5

Looking to upload an image file on the server using a servlet without utilizing HTML5. While browsing through stackoverflow, most answers I found were based on PHP. I attempted to read the image file at the client-side in JavaScript with FileReader.readAsD ...

The Angular-UI-Router version 1.0.11 is cautioning against the use of a class instead of an ID in the jarvis.widget.js file, stating "Warning: It appears you are using a

I recently upgraded my Angular version from 1.4.12 to angular v1.6.6 while using SmartAdmin. After the update, I noticed that an alert started appearing every time I switched between states where JarvisWidget was used. The alert is specifically triggered b ...

Retrieving content dynamically using ajax

As I load comments via ajax, I start with 5 by default and allow the user to request more. My query is centered around the best approach. What is the optimal location to construct the HTML elements meant for display on the page? Would it be better to cr ...

FadeOut/FadeIn Iframe Animation

Help needed with making an iframe fade in and out using jQuery. Something seems off with the code and I can't figure out why it's not working. Can anyone spot the mistake? HTML <body> <center> <div class="headbackground" ...

Achieving smooth transitions with CSS3 when removing a class

I've been attempting to implement animation transitions on a removed class, but unfortunately it's not working as expected. I referenced this link: CSS transition when class removed I have tried setting the transition on the parent element and o ...

Activate the function for items that are overlapping

Here is a scenario I am working on: HTML <body> <div> <p>Text</p> </div> <body> JQuery $('div').click(function(){ $(this).find('p').fadeToggle('fast'); }); $('bo ...

Transform a string into a boolean value for a checkbox

When using v-model to show checked or unchecked checkboxes, the following code is being utilized: <template v-for="(item, index) in myFields"> <v-checkbox v-model="myArray[item.code]" :label="item.name" ...

Insert text within the switch component using Material-UI

Looking to customize Material UI's Switch component with text inside? Check out the image below for an idea of what I'm going for. https://i.stack.imgur.com/KadRZ.png Take a look at my code snippet: import React from 'react'; import S ...

Rendering in ThreeJS Causes IE11 to Crash

I encountered a peculiar issue with Internet Explorer 11 while working on WebGL programming. Everything was functioning smoothly in all browsers until, out of the blue, IE started crashing when altering the positions of 4 meshes, without pointing to any sp ...