0

I have some script in javascript file call myjavascript.js and I want to get the content of asp:Label on my page. inside the head of my page i have linked the javascript file to the page. but am not able to reference the asp:Label om my page in myjavascript.js file. please help below is my code

//on my HTML page
<head>
 <script type="text/javascript" src="myjavascript.js"></script>
</head>

//in the myjavascript.js file
var totalamounttopay = document.getElementById('<%=Me.lblfinalgrosstotal.ClientID%>').innerText;
3
  • That's because myjavascript.js is sent as-is to the client and therefore asp.net never replaces the <% tag content. To make server-side data available to the client, you can either put it in an inline script or fetch() it from the client-side JS. The first way is very simple: insert <script>const theID = "<%=Me.lblfinalgrosstotal.ClientID%>";</script> above your existing script tag, then use theID in your script. Commented May 22, 2020 at 13:43
  • Does this answer your question? Passing variable from ASP.net to JavaScript Commented May 22, 2020 at 13:46
  • can u please demonstrate with the example I gave? Commented May 22, 2020 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.