33 users online (1 members and 32 guests)  


  Results 1 to 4 of 4

Related

  1. Javascript onclick Event Handler:    Forum: Javascript Forum
    Replies: 3
  2. returning values from scripts    Forum: CGI Perl Forum
    Replies: 4
  3. javascript onclick event to refresh iframe.    Forum: Javascript Forum
    Replies: 5
  4. Replies: 1
  1. #1
    gangleader's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    2

    returning value from an onclick event

    Hi,
    I have an external javascript function that returns a value. But im calling this function when ever user clicks a button

    (onclick event). How do i access that value in my html page.

    EXAMPLE:

    IN HTML:
    <input type="button" name="btn" onclick="addNumbers(var1, var2)"

    IN .JS FILE:
    function addNumbers(num1, num2)
    {
    return num1 + num2;
    }

    Now how do i access this value in HTML page.

    I appreciate if anyone could help me out.
    Thanks in advance

  2. #2
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Hi, welcome to the forums.

    Try
    Code:
    <input type="button" name="btn" onclick="result=addNumbers(var1, var2); alert(result);">
    And see what you get.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  3. #3
    gangleader's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    2
    Hi,
    Thanks for the reply.
    I have one more question.
    Is there any way i can access a java script variable in HTML page. Both java script and HTML are in seperate pages.

    EXAMPLE:

    IN HTML:
    <input type="button" name="btn" onclick="addNumbers(var1, var2)"

    IN .JS FILE:

    var num3;
    function addNumbers(num1, num2)
    {

    num3 = num1 + num2;
    }

    Now how do i access num3 value in HTML page. so instead of returning a value im trying to store that value in some java script variable and trying to access it in HTML.

    I appreciate if anyone can help me out.

    Thanks in advance

  4. #4
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    You can't access it directly in HTML, it has to be javascript.

    So, using my original example the variable is called result. You can write it to the page with

    Code:
    <script type="text/javascript">
    document.write(result);
    </script>
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?



Tags for this Thread