Search code examples
jqueryfileclientidexternal-js

External js file jquery function doesn't seem to get clientId


I use an externel javascript file and i have this,

function getdropdownvalue()
{
   alert($("#<%=DLState.ClientID%>"));
}

but it doesn't seem to get my dropdown's clientId... Any suggestion...


Solution

  • And is that dropdown in your external JavaScript file? If it's an external .js file, it has no idea about the fact that you may have a dropdown somewhere else on the internet.

    You need to pass the ClientID in from the page where you reference the JavaScript.

    .js file:

    function doStuff(selector) {
        // do something with $(selector)
    }
    

    or the jQuery way:

    jQuery.fn.doStuff = function() {
        return $(this).each(function() {
            // do something with $(this)
        }
    };
    

    .aspx file (after including your external JS):

    <script type="text/javascript">
        doStuff("#<%=DLState.ClientID%>");
    </script>
    

    By the way, if you just want to get the value of the dropdown, $("...").val() works quite fine.