0

I have this button in html:

<button id="btnCell" onclick="CellClicked(1, 1, false)"></button>

And this Javascript:

 $(function() {       
    $( "#txtSearch" ).autocomplete({
      source: function( request, response ) {
         //some logic
      },
      minLength: 2,
      select: function( event, ui ) {
        log( "Selected: " + ui.item.value + " aka " + ui.item.id );
        },
    });

  function CellClicked(x, y, selXml) {          
        //some logic
    }        
});

Whenever I click button above I get this error:

Uncaught ReferenceError: CellClicked is not defined

It happens because the method encapsulated in class, I know that I can drag the method out of the class and it will work.

But I want the method to be encapsulated those I ask the question how can I call the function when method CellClicked encapsulated?

Michael
  • 13,950
  • 57
  • 145
  • 288

1 Answers1

1

You can add the listener via Javascript, not via markup. It is more preferable than moving the function into the global scope.

 $(function() {       
    $( "#txtSearch" ).autocomplete({
      source: function( request, response ) {
         //some logic
      },
      minLength: 2,
      select: function( event, ui ) {
        log( "Selected: " + ui.item.value + " aka " + ui.item.id );
        },
    });

    function CellClicked(x, y, selXml) {          
        //some logic
    }       

    $('#btnCell').on('click', function() {
        CellClicked(1,1,false);
    }) 
});
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112