views:

348

answers:

1

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...

+3  A: 

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.

Matti Virkkunen
@matti no it is in my aspx page..
Pandiya Chendur
@Pandiya: How is your external JavaScript file supposed to know that you have a dropdown somewhere else on your site?
Matti Virkkunen
@matti i just did a hack and it worked...
Pandiya Chendur
@matti this is what i did `var dlstate = document.getElementById("ctl00_ContentPlaceHolder1_DLState");` and then `alert($(dlstate));`
Pandiya Chendur
@Pandiya: Yeah, that'll work... until the generated ClientID changes. The correct way is to always get it via the ClientID property.
Matti Virkkunen