Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net script in external js file

How can I use server-side script in an external js file to grab a reference to a dom element?

The following works as intended when used as inline-script, but is returning null when I move this to an external js file.

$("#<%= gridResults.ClientID %>");
like image 611
Jeremy Avatar asked Nov 19 '09 15:11

Jeremy


2 Answers

You'll need to have an inline script block that creates a JavaScript variable. This block should be added before your external JavaScript file. Once you do this, you can reference that variable in your external JavaScript file.

<script type="text/javascript">
    var grid = $("#<%= gridResults.ClientID %>");
</script>

<script type="text/javascript" src="path/to/my.js"></script>
like image 66
Tim S. Van Haren Avatar answered Oct 14 '22 20:10

Tim S. Van Haren


If you but a unique class on the grid using the CssClass property, you should be able to access the grid without having to know what it's clientID is.

like image 28
Chris Avatar answered Oct 14 '22 20:10

Chris