$find not working on a simple page

eferron
eferron used Ask the Experts™
on
Why does the variable 'c' always return null?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default9.aspx.cs" Inherits="Default9" %>
<!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>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
    <div>
    <script type="text/javascript">
    		function TestFx() 
    		{
    				var c = $find("<%= TextBox1.ClientID %>");
    				if (c == null) { alert("item is null"); } else { alert ("item is not null");	}
    		}
    </script>
    <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    </div>
    <asp:Button ID="Button1" runat="server" onclientclick="TestFx()" 
				Text="Button"  CausesValidation="false" UseSubmitBehavior="false"/>
    </form>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try $get instead. If I underestand their use correctly (been using for a long time still hard to find good details about them)...$find is used to find a component so you can find an ajaxcontroltoolkit component with it. While $get is a shortcut to find a DOM element which in this case is what you are looking for...

Also you may have trouble with <%= TextBox1.ClientID %>....if you do then you will have to assign the event to your button in code behind like this:

btn1.OnClientClick = "TestFx('" + TextBox1.ClientID + "')";

Then of course change your JS method to use that:
function TestFx(txtBox)
Commented:
<script type="text/javascript">
                function TestFx()
                {
                                var c = $get("<%= TextBox1.ClientID %>");
                                if (c == null) { alert("item is null"); } else { alert ("item is not null");    }
                }
    </script>

Commented:

This link give to information about what is difference between $get and $find.
http://mattberseth.com/blog/2007/08/the_everuseful_get_and_find_as.html

Author

Commented:
I actually punted and came up with the $get method earlier.  I will read the link jinal provided.  It is like $find was working on another page following the same pattern (strange).

Thanks all!

Commented:
$find work when there is component related to AajxControl toolkit.

like extender and all that.

thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial