JQuery - get a handle on a server element in javascript without using <%= elem.ClientID %>
One of the things I see a lot of is code that looks like this:
<asp:CheckBox ID="chkEnable" Text="My Checkbox" runat="server" /> <script type="text/javascript"> var chkEnable = $get("<%= chkEnable.ClientID %>"); </script>
You can see that the ClientID is being used, which will grab the exact element that .net generates, and will look like ctl00_plcMain_chkEnable or something like that.
jQuery can select an element by looking at what its attribute ends with, as explained here.
So we get this instead:
<asp:CheckBox ID="chkEnable" Text="My Checkbox" runat="server" /> <script type="text/javascript"> $(function() { var checked = $("input[id$=chkEnable]").attr("checked"); if (checked) { alert("It is checked. These are the droids you are looking for."); } }); </script>
The code in the selector gets all of the input elements that have an id attribute that ends with chkEnable. What really rocks (or sucks, depending on your point of view) is that if you have a repeater full of <asp:checkbox> controls, this will select all of them so you can process them.
More later - joel