Skip to content Skip to sidebar Skip to footer

How To Get Nested Control's Clientid Using Javascript?

Today I got an interview question as titled. For example, a Update Panel contains a Panel, the Panel contains a TextBox. How can I get the TextBox ClientID? The only I could thin

Solution 1:

You can access the control from a jQuery selector based on the end of the id like the example here:

<asp:CheckBoxID="chkEnable"Text="My Checkbox"runat="server" /><scripttype="text/javascript">
    $(function() {
        var checked = $("input[id$=chkEnable]").attr("checked");

    });
</script>

Here's an article for the exact thing: http://weblogs.asp.net/joelvarty/archive/2009/02/09/jquery-get-a-handle-on-a-server-element-in-javascript-without-using-lt-elem-clientid-gt.aspx

Solution 2:

Try this JQUERY

$("input[type='text']:first")

Example of Set focus on First Textbox

$(document).ready(function() {
       // focus on the first text input field in the first field on the page
        $("input[type='text']:first", document.forms[0]).focus();
    });

Solution 3:

You can try this

var $textboxes = $("input[type='text']")

Now, $textboxes is an array of jquery objects representing all textboxes on the page, so you can do something like

$.each($textboxes, function(index, obj) {
  // do something to check if this is the textbox that you are looking for...
});

Post a Comment for "How To Get Nested Control's Clientid Using Javascript?"