0

Well, I'm new to javascript and sap.net as well. I've been trying this but think the code isn't correct.

    <html>
<head runat="server">
<script type="text/javascript">
    function GetCal(lbl) 
    {
        var ddl = document.getElementById("<%=DropDownList1.ClientID%>");
        var SelVal = ddl.options[DropDownList1.selectedIndex].text;
        var a = parseInt("50");
        var data = a * SelVal;
        document.getElementById('lbl').innerHTML = data;

    }
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="GetCal(Label1)">
        <asp:ListItem Text="Select" Value="0" />
                     <asp:ListItem Text="1"/>
                     <asp:ListItem Text="2"/>
                     <asp:ListItem Text="3"/>
                     <asp:ListItem Text="4"/>
                     <asp:ListItem Text="5"/>
        </asp:DropDownList>
        <asp:Label ID="Label1" runat="server" Text="text"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="text"></asp:Label>
    </div>
    </form>
</body>
</html>

Here onchange="GetCal(Label1)" pass the label Id as parametere but this code doesn't change the text on Label1.

here document.getElementById('lbl').innerHTML = data; try to change the text. But I guess these are not the proper way, please help to know right syntax.

Wishing that I would be able to pass different label as parametere to the function, so that I can use different labels when needed and does change the text on the label by selecting items from DropDownList. And how to use the same function for many DropDownLists that. Say we have two DropDownList and I need perform same task on selecting items from DropDownList but here var ddl = document.getElementById("<%=DropDownList1.ClientID%>"); the function is only familiar DropDownList1.

So, what are the changes I need to make.

Expecting some help thanks.

1 Answer 1

0

You can simply use "this" keyword as following:

<asp:DropDownList ID="DropDownList1" runat="server" onchange="GetCal(this);">
    <asp:ListItem Text="Select" Value="0" />
                 <asp:ListItem Text="1"/>
                 <asp:ListItem Text="2"/>
                 <asp:ListItem Text="3"/>
                 <asp:ListItem Text="4"/>
                 <asp:ListItem Text="5"/>
    </asp:DropDownList>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.