Chrome browser not triggering first click event for AJAX jQuery checkbox

My Java application is built with Struts 1.1, utilizing actions, forms, and business objects. To enhance performance, I have integrated AJAX into my code to fetch data quickly and display it in a grid. However, I encountered an issue where clicking on a checkbox in a single row should open a new window, but the action only works from the second click in Chrome. Fortunately, the functionality works smoothly in IE and newer versions of Firefox. It's important to note that I am using jQuery 1.9.1 library. Here is a snippet of my JSP code:

// Function for handling checkbox clicks

function openWinForChequeDetails(f){
      $(document).ready(function(){
   // $('.chk').val($(this).is(':checked'));

   // Attach button click event handler 
             $('.chk').change(function () {

              // Loop through each checkbox in the table
               $("table tr td input[type=checkbox]");
                  // Check if checkbox is checked or not
                      if ($(this).is(':checked')) {
                      // If checked, retrieve the value
                          var v1 = $(this).parent().siblings('td').eq(1).text();
 var Asondate = document.postOutwardClearingChequesForm.asOnDate.value;
    var flg = 'Y';

 alert("Checkbox==>"+flg);

// Open a new window with details
winURL="/mybank/chequeDetailsPostOutwardClearingCheques.do?batchNumber="+v1+"&docDate="+Asondate+"&flag="+flg;
winName = "PostOutwardClearingChequesDetails";
winHeight = 380;
winWidth = screen.availWidth - 15;
winTop = ((screen.availHeight/2) - (winHeight / 6));

winLeft = 0;
winOpen = "";

if(winOpen){
if(winOpen.closed){
winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
} else { 
winOpen.close();
winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
}
} else {
winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
}
winOpen.focus();

 }   else{
 
var flg='N' ;
alert(flg);
$.ajax({
url:'/mybank/unChequeDetailsPostOutwardClearingCheques.do?batchNumber='+v1+'&docDate='+Asondate+'&flag='+flg,
            type:'post',  
            dataType: 'json'  
       
});

}Console.log("Exit Method"); 


  });
});
}
// AJAX Call within the gridview context

var rowNew= $('<td align="center" class="lbl-08" width="9%"></td>').html('<input type="checkbox" onclick="openWinForChequeDetails(postOutwardClearingChequesForm)" class="chk" value="Y" indexed="true" />').appendTo(tr);

Answer №1

Revise your JS function like this. Eliminate the need for $(document).ready or $('.chk').change, as you have directly called the function in the onclick event.

//This is the checkbox calling function............

function openWinForChequeDetails(f, elem){
      //Loop through each checkbox in table

                  //Check if checkbox is checked or not
                      if ($(elem).is(':checked')) {
                      //if checked then get the value
                          var v1 = $(elem).parent().siblings('td').eq(1).text();
                         var Asondate = document.postOutwardClearingChequesForm.asOnDate.value;
                     var flg = 'Y';

                alert("Checkbox==>"+flg);

    winURL="/mybank/chequeDetailsPostOutwardClearingCheques.do?batchNumber="+v1+"&docDate="+Asondate+"&flag="+flg;          
    winName = "PostOutwardClearingChequesDetails";
        winHeight = 380;
        winWidth = screen.availWidth - 15;
        winTop = ((screen.availHeight/2) - (winHeight / 6));

        winLeft = 0;
        winOpen = "";

        if(winOpen){
            if(winOpen.closed){
                winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
            } else { 
                winOpen.close();
                winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
            }
        } else {
            winOpen = window.open(winURL, winName, "top=" + winTop + ",left=" + winLeft + ",width=" + winWidth + ",height=" + winHeight + ",resizable=no,toolbar=no,scrollbars=yes,location=no,status=no,menubar=no")
        }
        winOpen.focus();

         }   else{

            var flg='N' ;
            alert(flg);
            $.ajax({
            url:'/mybank/unChequeDetailsPostOutwardClearingCheques.do?batchNumber='+v1+'&docDate='+Asondate+'&flag='+flg,
            type:'post',  
            dataType: 'json'  

        }); 

                }Console.log("Exit Method"); 



    }

Additionally, include the following Ajax call:

//This is an Ajax Call from gridview..................  

var rowNew= $('<td align="center" class="lbl-08" width="9%"></td>').html('<input type="checkbox"  onclick="openWinForChequeDetails(postOutwardClearingChequesForm, this)" class="chk" value="Y" indexed="true" />').appendTo(tr);

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

Internet Explorer experiencing problems with JSON when using ajax请求

After struggling with this issue for a significant amount of time, I have decided to seek help from the community. I am utilizing Google Custom Search REST API in combination with jQuery to retrieve results and display them in the browser. The problem I a ...

What steps should be taken to validate an HTML5 form without actually submitting it?

I am working on a form that includes various HTML attributes such as: <form> <label>Full Name</label> <input type="text" name="firstname" class="span3" required="required"> <label>Email Address</label&g ...

The event handlers on all elements are not loaded when the HTML page is rendered

Currently in the process of developing a project which involves a web server app built with nodejs + express and some client views that are rendered using ejs. In the main app.js file, the following code is present: const express = require('express& ...

Use Angular Js to add HTML inner elements to the table

This is my custom HTML code: <body> <div ng-app="tham" ng-controller="tham-control"> <div class="container"> <table class="table table-bordered table-striped"> <thead> ...

Retrieving information from an ajax array in PHP

I am currently attempting to retrieve an array of data using AJAX on the PHP side, but I am facing difficulties in accessing the values in PHP. Here is my JavaScript code snippet: console.log(obj); $.ajax({ method: 'POST', url: '/in ...

Is it possible to dynamically alter the value of "selectOneMenu" without having to refresh the "inputText" component concurrently?

Here is the code snippet: <h:form id="form" > <h:panelGrid > <p:inputText placeholder="Name" value="#{controladorGestionGrados.otherValue}" /> <p:selectOneMenu value="#{controladorGestionGrados.value}" > ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

Avoid having the last action repeat when the page is refreshed

I'm currently working with asp.net web forms update panel, and I've encountered an issue where the last request is repeated whenever I refresh a page. This causes an unintended effect where if I delete a record and then refresh the page, another ...

What could be causing the incorrect display of updates when my Grails checkbox onclick remote function Ajax call is triggered

Hi, I have a page named taskReminder that renders two templates: one for tasks and another for reminders. The task template includes a checkbox that, when checked, should update the task list. Everything is functioning correctly, but there seems to be an i ...

JavaScript struggles when dealing with dynamically loaded tables

I am currently working on integrating the javascript widget from addtocalendar.com into my website. The code example provided by them is displayed below. Everything functions as expected when I place it on a regular page. However, I am facing an issue wher ...

Unusual Behavior of JavaScript for..in and enum

I'm facing an issue with the peculiar behavior of the for..in loop in JavaScript. Here's the structure of my HTML: <div id="quarter_circle_top_left">...</div> <div id="quarter_circle_top_right">...</div> <div id="quart ...

Closing the JQuery login popup form by clicking on the submit button

I have successfully created a popup login form using jQuery. However, I am facing an issue where, upon clicking the submit button, the popup closes and displays an error message stating "wrong username and password". Ideally, I would like the popup form ...

What is the solution for resolving the problem of the cursor jumping to the end when converting numbers in JavaScript?

After exploring the inquiries regarding converting digits in JavaScript, such as What's the solution and the right way to convert digits in JavaScript? and How to convert numbers in JavaScript, and problems with commands to remove non-numeric characte ...

Frostyfire Ajax - Form Sending

I am seeking assistance with ColdFusion. I am interested in utilizing the CF9 ajax library. Currently, I have included <cfajaximport tags="cfform"> in my header and added a form using <cfdiv bind="url:domainchecker.cfm">. However, the entire fo ...

Wordpress AJAX request results in a value not found

** I have added more details, thank you for your assistance so far I am in the process of implementing an AJAX search functionality on my Wordpress site to filter properties based on type, location, and status. Being a novice at AJAX, I have been followi ...

Executing PHP code upon the successful completion of a jQuery Ajax request

How can I assign a value to a PHP variable after a successful ajax call? header.php <script> j.ajax({ method: 'get', url: '/php/ajax/auto_select_market.php', data: { 'city': geoi ...

The code is triggering IE 8 to switch to compatibility mode resembling IE7

I have created a custom javascript function that generates a pop-up, and I am invoking this function in my code. However, every time I click the button, the browser switches to IE 7 compatibility mode and the pop-up appears behind the button. Below is my ...

Retrieve the outcome of an AJAX request in JavaScript within a separate function

My uploadFunction allows me to upload files to my server using a Rest web service called with JQuery ajax. I also use this method for another function and need to determine the result of the web service call in order to add a row with the name of the uploa ...

Click on an element that is nested within another element that can also be clicked on

I'm facing a challenge while attempting to create an accordion inside another accordion. The issue arises with the nested elements and their behavior upon clicking. Essentially, I have a parent div with the class .applicant, which expands on click by ...

Working with Ruby on Rails by editing a section of embedded Ruby code in a .js.erb file

Currently, I am in the process of developing a single-page website and have successfully implemented ajax loading of templates to insert into the main content section. However, I am encountering difficulties when trying to do this with multiple templates u ...