user2284341
user2284341

Reputation: 671

Setting text of a label in javascript

Abstract: I'm trying to set an error message if a choice has not been made by a drop down menu.

Details:

Here's my dd with a call to a javascript function:

<%:Html.DropDownListFor(r => Model.TypeId, TypeItems, new { @class="stretchInput", @onblur = "errorCheck"})%>

Here's my javascript:

<script type="text/javascript">
    $(document).on("blur", "select[name=TypeId]",
        function errorCheck() {
            var errId = 'Type not chosen. Please choose from the menu.';
            if (document.getElementById('TypeId').selectedIndex == 0) {
                document.getElementById("DetailsError").visibility = 'visible';
                document.getElementById("DetailsError").innerHTML = errId;
            }
        });
</script>

Here's the label that I want to change the value of:

<td><asp:Label ID="DetailsError" runat="server" CssClass="errorMsg" Text="testing" Visible="false"></asp:Label></td>

I'm aware that it's an ASP control, by the way. I've tried HTML label and get the same result.

All I'm trying to set the label's text with my error message. It appears that nothing I do affects the label in any way.

What am I missing?

Upvotes: 0

Views: 2337

Answers (1)

Vivek Jain
Vivek Jain

Reputation: 3889

If you just have to use the label to display error messages from JavaScript, i.e. the control has no server side relevance, you may use <span id="DetailsError">.

Or

You may use the <%=DetailsError.ClientID%> everywhere in the HTML / JS code where you want to use DetailsError.

For e.g.:

document.getElementById("<%=DetailsError.ClientID%>").innerHTML = errId;

Please take this as a starting point and not as a copy-paste solution.

Upvotes: 2

Related Questions