Get the return value from a function in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashishc
    New Member
    • Jan 2008
    • 12

    Get the return value from a function in javascript

    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.

    [CODE=javascript]var returnValue="";
    formatButton = "<input type='radio' onClick='return getValue()' value='Radio2' name='btn2'>"

    function getValue()
    {
    return "selected button is 2";
    }
    [/CODE]
    Please let me know how to do this?

    Thanks
    Ashish
    Last edited by gits; Jan 28 '08, 07:47 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

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

    [CODE=javascript]// declare a variable:
    var a = '';

    // declare a function that returns a passed value
    function myfunc(param) {
    return param;
    }

    // to assign the returnvalue use:
    a = myfunc('test');

    // now we alert a that has 'test' assigned
    alert(a);
    [/CODE]
    kind regards

    Comment

    • ashishc
      New Member
      • Jan 2008
      • 12

      #3
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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

        Comment

        Working...