Jaie
Jaie

Reputation: 117

C# ASP.NET document.getElementById not getting elments

I have done searches for this but could not find an answer that worked.

I am trying to access the asp:TextBox ID="wordList" node details and its keep return count = 0. I have tried two alternate methods as can be seen below in the javascript and do not understand why its not working. Can anyone see the reason?

Many Thanks Jaie

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Week3.WebForm1" Theme="Theme1"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Grid Details</title>

</head>
<body>
    <script lang="javascript" type="text/javascript" >
        function ValidateWordList() {
            var x = document.getElementById("wordList").innerHTML;
            x += "Length" + x.length;
            alert(x);
            var z = document.getElementById('<%= wordList.ClientID %>').innerHTML;
            alert(z);    
            return false;
        }
    </script>
    <form id="form1" runat="server">
        <div class="form1Box">

        <br />
        <asp:Label ID="lblWord" runat="server" Text="Word(s) of Puzzle:"><asp:TextBox ID="wordList" runat="server" ClientIDMode="Static"/></asp:Label>
        <asp:RequiredFieldValidator ID="RequiredFieldValidatorWordList" ControlToValidate="wordList" 
             CssClass="Validation" runat="server" Text="(Required)" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidatorWordList" ControlToValidate="wordList" runat="server" CssClass="Validation"
              ValidationExpression="(^[a-zA-Z ,]*$)"  ErrorMessage="(The word(s) can only be letters, space or comma's!)"/> 

        <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Generate Puzzle" runat="server"  OnClientClick="return ValidateWordList()"/>

    </div>
    </form>
</body>
</html>

Upvotes: 0

Views: 3233

Answers (1)

Andrei
Andrei

Reputation: 56698

Are you trying to retrieve whatever value user has entered in the textbox? If so, you are probably looking for:

var x = document.getElementById("wordList").value;

Text fields do not define any inner markup, and their property innerHtml in most cases is just an empty string.

Upvotes: 3

Related Questions