locked
Javascript text into lable AFTER page load RRS feed

  • Question

  • User876561910 posted

    I have a page with a form and a dropdown - there dropdown is populated by code OnLoad

    But sometime there are no options to load and so the dropdown is empty.

    I am trying to add text to an ASP lable if this happens

    This can't happen until the page is folly loaded (to give the dropdown time to populate

    But it's not working - any suggestions ?

    Something like this

    <script language="javascript" type="text/javascript">
    function foobar(){
    var menu = getElementById("MyDDL");
    if(menu.options.length) {
    document.getElementById("MyLable").innerHTML = 'yes there are options to select';
    } else {
    document.getElementById("MyLable").innerHTML = 'no there arnt any options to select';
    }
    </script>
    
    
    <script language="javascript" type="text/javascript">
         window.onload = foobar();
    </script>
    

    Thursday, November 8, 2018 3:07 PM

Answers

  • User-821857111 posted

    var menu = getElementById("MyDDL");

    That should be document.getElementById 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 3:28 PM
  • User475983607 posted

    There are a few syntax and possible logical errors in the code.  Here's a working example.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsHtml.aspx.cs" Inherits="WebFormsApp.JsHtml" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DropDownList ID="MyDDL" runat="server">
    <%--            <asp:ListItem Value="1">1</asp:ListItem>--%>
            </asp:DropDownList>
            <asp:Label ID="MyLabel" runat="server" Text=""></asp:Label>
        </form>
        <script>
            function foobar() {
                var menu = document.getElementById("<%=MyDDL.ClientID%>");
                if (menu.options.length) {
                    document.getElementById("<%=MyLabel.ClientID%>").innerText = 'yes there are options to select';
                } else {
                    document.getElementById("<%=MyLabel.ClientID%>").innerText = 'no there arnt any options to select';
                }
            }
            window.onload = foobar();
        </script>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 3:34 PM

All replies

  • User2053451246 posted

    If using JQuery, call your code in a document ready function, not a page load function as you have done.

    http://learn.jquery.com/using-jquery-core/document-ready/

    Thursday, November 8, 2018 3:27 PM
  • User-821857111 posted

    var menu = getElementById("MyDDL");

    That should be document.getElementById 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 3:28 PM
  • User-821857111 posted

    If using JQuery, call your code in a document ready function, not a page load function as you have done.

    http://learn.jquery.com/using-jquery-core/document-ready/

    Can't see any jQuery?

    Thursday, November 8, 2018 3:29 PM
  • User475983607 posted

    There are a few syntax and possible logical errors in the code.  Here's a working example.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JsHtml.aspx.cs" Inherits="WebFormsApp.JsHtml" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DropDownList ID="MyDDL" runat="server">
    <%--            <asp:ListItem Value="1">1</asp:ListItem>--%>
            </asp:DropDownList>
            <asp:Label ID="MyLabel" runat="server" Text=""></asp:Label>
        </form>
        <script>
            function foobar() {
                var menu = document.getElementById("<%=MyDDL.ClientID%>");
                if (menu.options.length) {
                    document.getElementById("<%=MyLabel.ClientID%>").innerText = 'yes there are options to select';
                } else {
                    document.getElementById("<%=MyLabel.ClientID%>").innerText = 'no there arnt any options to select';
                }
            }
            window.onload = foobar();
        </script>
    </body>
    </html>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 3:34 PM
  • User876561910 posted

    Thank you all so much for taking the time to help me.

    J

    Thursday, November 8, 2018 4:17 PM