简体   繁体   中英

How to access js-variable in asp-markup from .js file (both executed on the same page)?

I have a little problem accessing a js variable, declared in the markup of an ascx control. The variable is declared like this:

var testValue = $('<%=leftProdTabPanel_LinkDiv_Values.ClientID %>');

Now I need to access this variable from another .js file which is loaded in the markup. I tried it using this way:

eval('testValue')

And this way:

window["testValue"]

But unfortunately it doesnt work. Is this even possible? If yes, do you have any hints for me?

Thank you very much!

If $ in your code stands for jQuery, than you need to prefix the ClientID value with a '#' as that is the selector prefix for ID queries. eg: $('#myid')

If the javascript assignment is executed in a closure than you will not be able to access it from other functions. for the sake of the test try removing var, sothat the variable will be placed in global scope for sure.

you can do the following,make the control ClientIdMode to static and use the Id in the java script example

and from js var lbl = $("#lblA").text();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM