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 badges1 Answer
Reset to default 3function 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/