ybbest

I try to access a the text of a textbox.But I dont know how to do it.After I create var d var d = document.getElementById("TextBox1");

I can't use d.value.I dont know why!Can anyone help!

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<script language="javascript" type="text/javascript">

function ValidateUserName()

{

var d = document.getElementById("TextBox1");

}

</script>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="TextBox1" runat="server" CausesValidation="True"></asp:TextBox>

<br />

<asp:Button ID="Button1" runat="server" Text="Button" CssClass="StandardButton"OnClientClick="javascript:return ValidateUserName();" /></div>

</form>

</body>

</html>



Re: JScript for the .NET Framework How do I get the value of a textbox.Can anyone help!

Figo Fei - MSFT

Code Snippet

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<script language="javascript" type="text/javascript">

function ValidateUserName()

{

var d = document.getElementById("TextBox1");

window.alert(d.value);

}

</script>

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="TextBox1" runat="server" CausesValidation="True"></asp:TextBox>

<br />

<asp:Button ID="Button1" runat="server" Text="Button" CssClass="StandardButton" OnClientClick="ValidateUserName();" /></div>

</form>

</body>

</html>

The code works well.

d.value refer to the text of the TextBox1

Check it out.

Thanks






Re: JScript for the .NET Framework How do I get the value of a textbox.Can anyone help!

RRocket

I tried to use something similar to that to get a value out of a text box, but get "document.getElementById('tbDescription') has no properties" in FireFox and "object required" in IE.

[code]

<asp:TextBox id="tbDescription" runat="server" Height="78px" TextMode="MultiLine" onkeypress="check()" Width="363px"></asp:TextBox>

<asp:RequiredFieldValidator id="rfvDescription" EnableClientScript="true" runat="server" ControlToValidate="tbDescription"

Display="none" ErrorMessage="You must give a description of your shipment." Font-Bold="True"

SetFocusOnError="True" ValidationGroup="quickQuote"></asp:RequiredFieldValidator>

<cc1:ValidatorCalloutExtender id="co_rfvDescription" runat="server" TargetControlID="rfvDescription"></cc1:ValidatorCalloutExtender>

[/code]

Javascript Function

[code]

<script language="javascript" type="text/javascript">

function check()

{

var x = document.getElementById('tbDescription').value;

alert(x);

}

</script>

[/code]

Thanks....