How to return value from Javascript Function ?


JSFunction
Hi

Function is the heart of java script. If you will do anythings in java script then you have to write function.

While working on asp.net project so many time we get scenario to retun value from some function and display somewhere.

But here we can do like C# method. We have to include retun at end of function like this


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Function.aspx.cs" Inherits="Javascript_Test_Function" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function sayHello() {
            var msg = "I m from Javacript";
            return msg;
        }

        function doSomething() {
            var msg1 = sayHello();
            document.getElementById('<%=txtMsg.ClientID %>').value = msg1;
            
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnclick" runat="server" OnClientClick="doSomething();return false;" Text="Click Here" />
    <br />
    <br />
    <asp:TextBox ID="txtMsg" runat="server" />
    </div>
    </form>
</body>
</html>


Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.