Having issues with clicking on a row in the table while using AJAX functionality

Experiencing a puzzling issue while attempting to add click functionality to table rows with AJAX, here is the JavaScript code used:

//for tabs
$(document).ready(function () { 
    $("#tabs").tabs();
});

$(window).load(function() {
    jsf.ajax.addOnEvent(function (data) {
        if (data.status === "success") {
            $("#tabs").tabs();
        }
    });
});

$("tr").not(':first').hover(
    function () {
        $(this).toggleClass('highlited');
    //                $(this).css("background","#707070");
    }, 
    function () {
        $(this).toggleClass('highlited');
    //                $(this).css("background","");
    }
    );              

function highlight(param) {  
    var row = jQuery(param).parent().parent().children();
    row.toggleClass('highlited');
//                row.css("background","#707070");
}        

var inputs = document.getElementsByTagName("INPUT");
for (var i in inputs) {
    if (inputs[i].checked) {
        highlight(inputs[i]);
    }
}

//for clicking on a row and opening new page
function addOnclickToDatatableRows() {
    //gets all the generated rows in the html table
    var trs = document.getElementById('form:dataTable').getElementsByTagName('tbody')[0]
    .getElementsByTagName('tr');
    //on every row, add onclick function (this is what you're looking for)
    for (var i = 0; trs.length > i; i++) {
        var cells = trs[i].cells;                    
        for(var j=1; j < cells.length; j++){
            cells[j].onclick = new Function("rowOnclick(this.parentElement)");
        }                    
    }
}

function rowOnclick(tr) {
    var elements = tr.cells[0].childNodes;
    for(var i = 0; elements.length > i; i++) {                           
        if ((typeof elements[i].id != "undefined") && (elements[i].id.indexOf("lnkHidden") > -1)) {
            //opne in a new window//  window.open(elements[i].href);
            location.href=elements[i].href
            break;
        }
    }                
    return false;
}

This is the JSF page setup:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"    
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>test</title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <link rel="shortcut icon" type="image/x-icon" href="resources/css/themes/nvidia.com/images/favicon.ico" />
            <link href="resources/css/helper.css" media="screen" rel="stylesheet" type="text/css" />
            <link href="resources/css/dropdown.css" media="screen" rel="stylesheet" type="text/css" />
            <link href="resources/css/default.advanced.css" media="screen" rel="stylesheet" type="text/css" />

            <script type="text/javascript" src="resources/js/jquery-1.7.2.min.js"></script>
            <script type="text/javascript" src="resources/js/jquery-ui-1.8.18.custom.min.js"></script>
            <link href="resources/css/jquery-ui-1.8.18.custom.css" media="screen" rel="stylesheet" type="text/css" />
            <script type="text/javascript" src="resources/js/tabs.js"></script>          
    </h:head>
    <h:body onload="addOnclickToDatatableRows();">

        <h1><img src="resources/css/images/icon.png" alt="DX-57" /> History Center</h1>
        <div id="toolbar" style="margin: 0 auto; width:1180px; height:30px; position:relative;  background-color:black">
            <!-- Include page Navigation -->
            <ui:insert name="Navigation">           
                <ui:include src="Navigation.xhtml"/>         
            </ui:insert>

        </div>  

        <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
            <h:graphicImage alt="Dashboard"  style="position:relative; top:-20px; left:9px;"  value="resources/images/logo_sessions.png" />
        </div>
        <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute;  background-color:transparent; top:105px">

            <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute;  background-color:transparent; top:80px">

                <div id="settingsHashMap" style="width:750px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

                    <h:form id="form">
                        <h:dataTable id="dataTable" value="#{SessionsController.dataList}" binding="#{table}" var="item">
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText value="Select" />
                                </f:facet>
                                <h:selectBooleanCheckbox onclick="highlight(this)" value="#{SessionsController.selectedIds[item.aSessionID]}" />
                                <h:outputLink id="lnkHidden" value="HistoryLink.xhtml" style="display:none">
                                    <f:param name="id" value="#{item.aSessionID}" />
                                </h:outputLink>
                            </h:column>
                            <h:column>
                               ...
                            </h:column>
                        </h:dataTable>
                        ...

                        //Additional buttons and features added here

                    </h:form>                  

                </div>   
            </div>  
        </div>

    </h:body>
</html>

Facing issues with the AJAX implementation where clicking on datatable rows only works when initially loaded but breaks when switching pages. Is it possible that calling the JavaScript code twice in different locations may be causing the problem?

Answer №1

Implement the onmouseover function.

<h:dataTable onmouseover="addOnclickToDatatableRows();">

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

Passing references in React to parent components

Struggling to adopt the react way of thinking, I'm facing an issue with invoking the .submit() method of the form component. Within a material-ui Dialog, buttons are passed via the actions property. Now, I need to trigger the .submit() method of the ...

What could be causing useEffect to trigger only after the component has been mounted in NextJS?

I'm currently encountering an issue with my implementation of a useEffect function that is supposed to act like a componentWillMount, but the behavior is not as expected. Within the code for Component (as demonstrated in the Codesandbox provided belo ...

Activate the element by simulating a button click on another element

I created a "copy-button" component that is utilized in various parts of the application, allowing users to copy information to their clipboard. This component has two bindings: buttonText and buttonClass, which can be used to customize the text displaye ...

"Discover the process of transforming HTML, CSS, and script files into a cohesive JavaScript format

I have developed a unique web chat widget from scratch using HTML, CSS, JavaScript, and AJAX calls. Now, my goal is to convert it into a script that can be easily embedded in any other websites or webpages. Similar to how third-party widgets work, users sh ...

Showing the number of times a button has been pressed

I have written some HTML code to create a button and now I am looking for guidance on how I can use Vue.js to track how many times the button has been clicked. Here is what I have so far: <div class="123"> <button id = "Abutton&q ...

Encountering a User Agent error while trying to update Vue to the latest version using N

I was interested in experimenting with staging.vuejs. When I tried to install it using the command npm init vue@latest, I encountered an error. Here is the link for reference: https://i.stack.imgur.com/rCipP.png SPEC Node : v12.13.0 @vue/cli : v4.5.15 ...

Ways to retrieve only the chosen option when the dropdown menu features identical names

How can I retrieve only the selected value from the user, when there are multiple select options with the same class name due to dynamic data coming from a database? The goal is to submit the data using Ajax and have the user select one option at a time. ...

Integrate JavaScript code with HTML pages

After creating a basic HTML structure that I am proud of, I encountered some challenges with getting the Javascript to function properly. I am new to working with Javascript and received some initial assistance, but unfortunately, it is no longer working. ...

How can I modify my code to ensure that trs and th elements are not selected if their display property is set to none?

I am currently working on creating a filter for my pivot table, but I am unsure of how to dynamically calculate the sum of each row/column based on whether they are displayed or not. If you need any other part of my code, feel free to ask. To hide employee ...

What causes the transformation of a POST request into a GET request?

Upon utilizing jQuery's $.ajax() or $.post() method to transmit form data to the server, I've noticed that the 'data' string gets appended to the end of the URL. This seems to be causing the POST request to transform into a GET request. ...

Error encountered while attempting to convert CSV file: InvalidStateError

I've implemented a JavaScript function to be triggered by the 'onclick' event of an HTML button: function exportReportData2() { if ($("#Report").val() != "") { var screenParametersList = buildScreenParameters(); var ...

Refreshing a Web Element using Selenium's handle

I am currently conducting a Website testing project that involves multiple frames. There is a possibility that the server may regenerate a frame at any given time, even if no changes have been made to any element within it. When this occurs in the midst of ...

JQuery is having issues with $(this) and the find function not working properly

I am attempting to access a child of the div with the class form-group, specifically, I want to display the value of the input element. <div class="form-group"> <label>text</label> <input name="text" type="text" class="form-co ...

Errors related to Typescript are causing issues with the stock installation of Next.js

Whenever I use typescript with create-next-app, my tsx files are filled with numerous "Problems" messages. These errors only appear in Visual Studio Code and do not affect the build process. I have tried uninstalling vscode, removing all extensions, and ...

Learn how to fetch user-selected options in Node.js and display the corresponding file contents in a textarea after submission

Hello, I am new to Node.js so please be patient with me. I am struggling to figure out how to retrieve the selected option from a drop-down list after a user submits it and then perform an action based on that choice. Here is an example of what I have in m ...

React Hook is failing to trigger an update

Learning React and JavaScript has been quite a challenge for me, especially when it comes to understanding React Hooks and the issue of them not updating sometimes. I have tried searching online but either end up with solutions for class-based components o ...

Advantages and Disadvantages of Implementing Ajax for Form Validation

Exploring the benefits of validating forms with Ajax, I find it to be a valuable tool in preventing code redundancy. However, before making the switch, I am curious about any potential drawbacks compared to traditional JavaScript validation. While I have c ...

Running the Express service and Angular 6 app concurrently

Currently, I am in the process of developing a CRUD application using Angular6 with MSSQL. I have managed to retrieve data from my local database and set up the necessary routes, but I am encountering difficulties when it comes to displaying the data on th ...

Acquiring recognizable incorrect XML using Selenium automation technology

Currently, I am facing a challenge with my Selenium test that involves retrieving raw XML from a web server. The issue arises when one of the XML documents is missing a root element, making it invalid. My objective is to acquire the original source of the ...

Exploring the journey of History.js and Same-Origin Policy leading to HTTPS encryption

Utilizing history.js, I am attempting to push a state change from an HTTP site like: http://www.example.com/some/resource ...to a secure site (payment page), such as: https://www.example.com/payment/for/some/resource However, Safari is throwing thi ...