javascript - how to print the return value of a function in jquery -
i want print return value function
$(document).ready(function () { $('.click').click(function (e) { var value = 1; check(value); //function call value:value=1 alert(val); //here want return value function }); function check(value) { if (value == 1) { var val = 'success'; return val; } else { var val = 'error'; } } }); //document ready function so how acieve return value of function , print value of function returned
you this:
var value = 1; var valofvalue = check(value); //function call value:value=1 alert(valofvalue); //here want return value function or, shorter:
var value = 1; alert(check(value)); the reason wasn't working because val not global variable - made in check() function , no 1 other things inside check() can access it. made variable, valofvalue, store return value , use in click function.
even better, save making multiple variables, put val variable way @ top of function, don't need return anything:
$(document).ready(function () { var val = ''; $('.click').click(function (e) { var value = 1; check(value); alert(val); }); function check(value) { if (value == 1) { val = 'success'; } else { val = 'error'; } } });
Comments
Post a Comment