0

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?

3
  • 1
    View the generated source of the page, look for the label. What do you see? Commented Jul 3, 2013 at 18:04
  • <span id="MainContent_DetailsError" class="errorMsg">testing</span> Commented Jul 3, 2013 at 19:26
  • BINGO! I prefixed DetailsError with 'MainContent_' and it works. Thanks epascarello! Commented Jul 3, 2013 at 19:28

1 Answer 1

2

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.

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.