Why, in this...">

Asp: RadioButton and javascript $ find () function

<asp:RadioButton ID="myButton" Text="Option" GroupName="group" runat="server" /> 

Why, in this case, does the javascript function call $find('<%= myButton.ClientID %>') return null ? I need to detect / change value in javascript client. $find() should be a shortcut for Sys.Application.findComponent() .

UPDATE: No problem with identifier, I tried $ find () in the chrome console with a real rendered identifier and still get null. And I want to get an Ajax control, not a DOM element.

+4
source share
4 answers

You need to set clientIdMode to static if you don't have multiple instances:

 <asp:RadioButton ClientIDMode="static" ID="myButton" Text="Option" GroupName="group" runat="server" /> 

OR

You need to use ClientID to access your button. But the script should be on the same page.

 var radioButton = $('#<%= myButton.ClientID %>'); 
+2
source

You can directly evaluate an element by its identifier, and you need not use find , you just need the id selector, since id is unique to html elements.

 $(function(){ var myButton = $('#<%= myButton.ClientID %>'); alert(myButton.id); //with javascript myButton = document.getElementById('#<%= myButton.ClientID %>'); }); 
0
source

Are you sure your DOM is ready when you try to get a component?

Try something like this:

 window.onload = function() { alert( $find('myButton') ); } 
0
source

$ find () tries to find the ASP.NET AJAX component from its behavior identifier.

Since you are looking for a DOM element, not a component, instead of $ get () :

 var yourButton = $get("<%= myButton.ClientID %>"); 
0
source

All Articles