Search code examples
javascriptjqueryeventsonloadhandsontable

How to Onload() set focus to jQuery Handsontable


What is the proper method to set the focus onload to first field in jQuery Handsontable ?

Here is the example link http://jsfiddle.net/4kvMq/4/

$(function () {
  $(window).load(function () {
    $('#example10grid table tbody tr td:first-child').focus();
  });
});

Even have tried to set focus manually but no luck!

$('#btnGo').click(function() {
    $(".dataTable table tbody tr td:first-child").focus();
});

Many, many thanks!


Solution

  • Thanks for your request. Now there is a public method to do that.

    Use it on a button:

    $('#btnGo').click(function(event) {
      setTimeout(function() {
          //timeout is needed because Handsontable normally deselects 
          //current cell when you click outside the table
          $("#example10grid").handsontable("selectCell", 0, 0);
      }, 10);
    });
    

    Or DOM ready:

    $(function() {
      setTimeout(function() { 
          //timeout is needed because Handsontable normally deselects 
          //current cell when you click outside the table
          $("#example10grid").handsontable("selectCell", 0, 0);
      }, 10);
    });
    

    Live example: http://jsfiddle.net/warpech/4kvMq/35/