How To Return Value From Jquery Function?
I am trying to return value from jQuery function. Here is my function: $('#output').keypress(function (event){ var keycode = (event.keyCode ? event.keyCode : event.which);
Solution 1:
In event handlers, you should call other functions instead of returning a value. function(){}
in your event handler is a callback
function.
In your case, you can directly print them in the keypress
event:
$('#output').keypress(function (event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var read_lines = $('#output').val().split("\n");
alert(read_lines[read_lines.length-1]);
}
});
Or you can do this:
$('#output').keypress(function (event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var read_lines = $('#output').val().split("\n");
MyFunction(read_lines[read_lines.length-1]);
}
});
functionMyFunction(result){
alert(result);
}
Solution 2:
In order to access your read value, you need function similar as below.
functionName(newRead){
alert(newRead);
}
$('#output').keypress(function (event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
var read_lines = $('#output').val().split("\n");
functionName(read_lines);
//return read_lines[read_lines.length-1];
}
});
Post a Comment for "How To Return Value From Jquery Function?"