javascript - How to assign value returned by a function call inside another function [JS] - Stack Overflow

I am having two functions in my JavaScript code, and I want to perform some action on the basis of valu

I am having two functions in my JavaScript code, and I want to perform some action on the basis of value returned by other function.

Here my code :-

function test1(){
    var radio = document.getElementByName('sample');
    for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = return test1();
    // some code and work according to the value in returnValue
}

Well I know return in function test2 is not correct. so what can I do here NOW????


Edit

Here what I am Trying to do... but it do not seem to be working /

EDIT2 ----- The corrected and working fiddle... / Thanks to all :)

I am having two functions in my JavaScript code, and I want to perform some action on the basis of value returned by other function.

Here my code :-

function test1(){
    var radio = document.getElementByName('sample');
    for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = return test1();
    // some code and work according to the value in returnValue
}

Well I know return in function test2 is not correct. so what can I do here NOW????


Edit

Here what I am Trying to do... but it do not seem to be working http://jsfiddle/U6RjY/7/

EDIT2 ----- The corrected and working fiddle... http://jsfiddle/U6RjY/9/ Thanks to all :)

Share Improve this question edited Dec 28, 2012 at 13:35 Sanuj asked Dec 28, 2012 at 12:40 SanujSanuj 1,1571 gold badge12 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3
function test1(){
    var radio = document.getElementByName('sample');
     for (i=0; i<radio.length; i++){
         //some code here
         return "some value on basis of above code"
    }
}

function test2(){
    var somevariable = globllysetValue;
    var returnValue = test1();
    // some code and work according to the value in returnValue
}

Just remove the return.

You will notice however that in function test1, it will return the value on the first loop. So, it will stop executing.

Live example: http://jsfiddle/U6RjY/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140859a4613424.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信