javascriptjqueryfocusevent-bubblingevent-capturing

How to set Focus on Input using Tab Key Press in Next Table Cell


Following are scenarios I m implementing using JQuery in HTML

In Case of 2 Scenario, I need to trigger the dblclick() event on the next Cell which creates the Input Element Inside it, but it is not able to the Focus inside Input Element.

I have tried to debug that scenario not able to understand why it is happening.

I have created a button and written click function which trigger dblclick() , then focus was set inside the Input Element.

I m not able to understand when user click using the mouse / click() then why focus is set on the created input element but why same is not happening when I m triggering double click when from the code using dblclick()

Following are code For the Same:


//Global Variables
var TabEdit_sourceInput = null;
var TabEdit_currentTd = null;
var TabEdit_DOMtocheck = null;
var TabEdit_previousHour = null;
var gridRefreshFlag = false;

//Init Double Click Event on the Table TD
$('.custom-table-td').dblclick(function(event){
    var tableId = $(this).attr('id');
    var inputId = 'input-'+tableId;
    $(this).html('<input id="input-'+tableId+'" placeholder="Type Message" class="form-control" onkeydown="return initCheckKeypress();" tabindex="1"/>');
    
    //Focusing on Generated Event
    $('#'+inputId).focus();

    //Binding the Focus Out Event to
    $('#'+inputId).focusout(function(){
        debugger;
        $('.header').html($(this).val());
     });
     
});

//Click Event to trigger dblclick on td with id = 'tableData1'
$('.btn-trigger-dbclick').click(function(){
    $('#tableData1').dblclick();
});

//Function to check Tab is Press or not
function initCheckKeypress(evt) {
    "use strict";
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if (charCode === 9) {
        gridRefreshFlag = true;
        TabEdit_sourceInput = $(e.target);
        TabEdit_currentTd = TabEdit_sourceInput.parent();
        TabEdit_DOMtocheck = TabEdit_currentTd.next('td');
        movetoNextTD();
    }
}


//Code to Click on Next Cell
function movetoNextTD() {
    if (gridRefreshFlag && TabEdit_DOMtocheck !== null) {
        TabEdit_currentTd.html("");
        TabEdit_DOMtocheck.dblclick();
        resetmovetoNextVariable();
    }
}

//Reset Global Variable
function resetmovetoNextVariable(){
    TabEdit_sourceInput = null;
    TabEdit_currentTd = null;
    TabEdit_DOMtocheck = null;
    TabEdit_previousHour = null;
    gridRefreshFlag = false;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Focus Issue</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" >
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <div class="header"></div>
                <table class="table table-bordered">
                    <tbody>
                        <tr id="tableRow1">
                            <th id="tableHeader1" class="custom-table-th">TH 1</th>
                            <th id="tableHeader2" class="custom-table-th">TH 2</th>
                            <th id="tableHeader3" class="custom-table-th">TH 3</th>
                        </tr>
                        <tr id="tableRow2">
                            <td id="tableData1" class="custom-table-td"></td>
                            <td id="tableData2" class="custom-table-td"></td>
                            <td id="tableData3" class="custom-table-td"></td>
                        </tr>
                    </tbody>
                </table>
                <button class="btn btn-primary btn-trigger-dbclick">Trigger</button>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>


Solution

  • What's happening is this:

    The fix is to cancel the system default event after creating the 2nd input:

    e.preventDefault();
    

    Updated snippet:

    //Global Variables
    var TabEdit_sourceInput = null;
    var TabEdit_currentTd = null;
    var TabEdit_DOMtocheck = null;
    var TabEdit_previousHour = null;
    var gridRefreshFlag = false;
    
    //Init Double Click Event on the Table TD
    $('.custom-table-td').dblclick(function(event){
        var tableId = $(this).attr('id');
        var inputId = 'input-'+tableId;
        $(this).html('<input id="input-'+tableId+'" placeholder="Type Message" class="form-control" onkeydown="return initCheckKeypress();" tabindex="1"/>');
        
        //Focusing on Generated Event
        $('#'+inputId).focus();
    
        //Binding the Focus Out Event to
        $('#'+inputId).focusout(function(){
            debugger;
            $('.header').html($(this).val());
         });
         
    });
    
    //Click Event to trigger dblclick on td with id = 'tableData1'
    $('.btn-trigger-dbclick').click(function(){
        $('#tableData1').dblclick();
    });
    
    //Function to check Tab is Press or not
    function initCheckKeypress(evt) {
        "use strict";
        var e = event || evt; // for trans-browser compatibility
        var charCode = e.which || e.keyCode;
        if (charCode === 9) {
            gridRefreshFlag = true;
            TabEdit_sourceInput = $(e.target);
            TabEdit_currentTd = TabEdit_sourceInput.parent();
            TabEdit_DOMtocheck = TabEdit_currentTd.next('td');
            movetoNextTD();
            e.preventDefault();
        }
    }
    
    
    //Code to Click on Next Cell
    function movetoNextTD() {
        if (gridRefreshFlag && TabEdit_DOMtocheck !== null) {
            TabEdit_DOMtocheck.dblclick();
            TabEdit_currentTd.html("");
            resetmovetoNextVariable();
        }
    }
    
    //Reset Global Variable
    function resetmovetoNextVariable(){
        TabEdit_sourceInput = null;
        TabEdit_currentTd = null;
        TabEdit_DOMtocheck = null;
        TabEdit_previousHour = null;
        gridRefreshFlag = false;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Focus Issue</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" >
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <div class="header"></div>
                    <table class="table table-bordered">
                        <tbody>
                            <tr id="tableRow1">
                                <th id="tableHeader1" class="custom-table-th">TH 1</th>
                                <th id="tableHeader2" class="custom-table-th">TH 2</th>
                                <th id="tableHeader3" class="custom-table-th">TH 3</th>
                            </tr>
                            <tr id="tableRow2">
                                <td id="tableData1" class="custom-table-td"></td>
                                <td id="tableData2" class="custom-table-td"></td>
                                <td id="tableData3" class="custom-table-td"></td>
                            </tr>
                        </tbody>
                    </table>
                    <button class="btn btn-primary btn-trigger-dbclick">Trigger</button>
                </div>
            </div>
        </div>
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
    </body>
    </html>