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

I am using externel javascript file and I have this,

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

      

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

+2


a source to share


1 answer


And is this a dropdown in your external JavaScript file? If it is an external .js file, it has no idea that you might have a dropdown elsewhere on the internet.

You need to pass in the ClientID from the page you are linking to 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 (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 pretty well.

+5


a source







All Articles