最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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/

发布评论

评论列表(0)

  1. 暂无评论