0

I am using asp.net web forms and have an event fired when some drop down selection changes. Event is not in the code behind , instead it is a javascript method that is executed when the event fires. I need to change the text of some label according to the value changed in the dropdown.

I am a newbie to javascript, couldn't find a way to access the "Text" property of the label. Can someone help pls ?

<script type="text/javascript">
    function myMethod(sender, args) {

        ............
    }
</script>

2 Answers 2

1

Here a quick example. The most important thing to remember is that aspnet can rename the ID of the element in the generated html. So always use ClientID

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
</asp:DropDownList>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


<script type="text/javascript">
    $('#<%= DropDownList1.ClientID %>').change(function () {
        $('#<%= Label1.ClientID %>').text($(this).val());
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

In html you can call the event by putting a event using onchange attribute. you can get the value in JavaScript by using this keyword. for example I make a small demo here https://jsfiddle.net/3nfvy6ke/5/

<select onchange="javascript:test(this)">
<option value="1">1</option>
<option value="2" selected>2</option>
</select>
<script>
function test(ele){
debugger;
document.write(ele.value);
}
</script>

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.