By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Get the return value from a function in javascript

P: 12
Hi
I want to know how can I get the return value from a function into a variable onClick event of a selection of radio button.

Let me explain in detail, I have 3 radio buttons and if i select any one, then I want to call a function to which I pass the value of selected radio button and the function does some processing and then returns some value which i store in a variable.

Expand|Select|Wrap|Line Numbers
  1. var returnValue="";
  2. formatButton = "<input type='radio' onClick='return getValue()' value='Radio2' name='btn2'>" 
  3.  
  4. function getValue()
  5. {
  6. return "selected button is 2";
  7. }
  8.  
Please let me know how to do this?

Thanks
Ashish
Jan 28 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,333
hi ...

that is very basic and simple have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. // declare a variable:
  2. var a = '';
  3.  
  4. // declare a function that returns a passed value
  5. function myfunc(param) {
  6.     return param;
  7. }
  8.  
  9. // to assign the returnvalue use:
  10. a = myfunc('test');
  11.  
  12. // now we alert a that has 'test' assigned
  13. alert(a);
  14.  
kind regards
Jan 28 '08 #2

P: 12
That is not what I want..I can do that too but what I want is when i select a radio button, it invokes the onClick event. Now onClick of radio button I want a function invoked which returns a value and that is stored in a variable.
Jan 28 '08 #3

gits
Expert Mod 5K+
P: 5,333
that is the same:

[HTML]<input type="radio" onclick="var ret_val = getValue();" value="Radio2" name="btn2">
[/HTML]
this assigns the return-value that the function getValue() returns to a global variable called ret_val

kind regards
Jan 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.