user1801974
user1801974

Reputation: 31

Unable to get javascript value from label

I'm a newbie in programming thus do pardon me.

I have a .js game that outputs a variable called "score".

I require the variable to be able to be displayed on my asp label and after which grabbing its value for database storage onClick()

Currently what I have been able to do were:

@ .js:

    document.getElementById('score').value = score;

@ .aspx:

    <asp:Label runat="server"  ID="score"></asp:Label>

The score is reflected on this label right now. However, I need to do a additional function which is to obtain the value of 'score' from the label and use it as a String in C#

Upvotes: 3

Views: 1659

Answers (2)

Sooraj
Sooraj

Reputation: 41

Could you please try the below suggession.

in .js

document.getElementById('<%= lblScore.ClientID %>').innerHTML = 'Test Value';
document.getElementById('<%= hdnScore.ClientID %>').value = 'Test Value';

in .aspx

<asp:Label runat="server" ID="lblScore"></asp:Label>
<asp:HiddenField ID="hdnScore" runat="server" />

onclick event

var score = hdnScore.Value;

Hope this will help you to fix the issue.

Upvotes: 1

Adil
Adil

Reputation: 148110

Use ClientID of server control as id score would have changed in the generated html by asp.

Change

document.getElementById('score').value = score;

To

document.getElementById('<%= score.ClientID %>').value = score;

Upvotes: 3

Related Questions