user478636
user478636

Reputation: 3424

Calling an external function inside a function in jQuery

$(function () {
  $('#cmd').bind('keydown', function (evt) {
    if (evt.keyCode === 13) {


//* I want to call the function here *//
    }
    });

});

This is the function i want to call wit the parameter.

$(function (String msg) {

var cmdStr = msg;

    $.ajax({
        url: 'exec.php',
        dataType: 'text',
        data: {
            q: cmdStr
        },
        success: function (response) {
            $('#txtOut').append(response);
        }

    });

}
});

});

Upvotes: 1

Views: 2519

Answers (2)

S L
S L

Reputation: 14318

$('#cmd').keydown(
    function (event){
        if(event.keyCode == 13){
        event.preventDefault(); 
        /*you can call your function here*/
        $.ajax({
          url: "exec.php",
          method: "get",
          dataType: "text", 
          data: { q: $('#com').val()},
          success: function(data){ 
             $('#txtOut').append(response);
          }
        });
        /*still you can it here*/
      }
    }
);

Upvotes: 2

Ivo
Ivo

Reputation: 3436

Put the function outside the document ready and give it a name.

function MyFunction(msg)
{
var cmdStr = msg;
$.ajax({
    url: 'exec.php',
    dataType: 'text',
    data: {
        q: cmdStr
    },
    success: function (response) {
        $('#txtOut').append(response);
    }

});

}

then call it from your click event

  $(function () {
  $('#cmd').bind('keydown', function (evt) {
    if (evt.keyCode === 13) {    

       MyFunction("message");
    }
    });

});

Upvotes: 2

Related Questions