views:

1322

answers:

3

I have two labels: username and email. I use them to retrieve their content and pass them as method parameters.

var lblEmail = $get('<%= lblEmail.ClientID %>');
var email = lblEmail.innerText;

var lblUsername = '<%= lblUsername.ClientID %>';
var username = lblUsername.innerText;

The email label is defined in the same way as username label does, but for this last I can't get the value, innertext returns undefined.

...

<td class="inputField">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<asp:Label ID="lblUsername" CssClass="font1" runat="server"/>
</tr>
</table>
</td>

...

...
<td class="inputField">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:Label runat="server" ID="lblEmail" CssClass="font1" />
</td>
...

What could be a possible reason to justify what is happening? How can I solve this problem? I'm seeing on the page both label values, I set them on the same place (when page loads).

+3  A: 

Because you defined only the client ID for the userName label :

var lblEmail = $get('<%= lblEmail.ClientID %>');
var email = lblEmail.innerText;

var lblUsername = '<%= lblUsername.ClientID %>';
var username = lblUsername.innerText;
Canavar
OMG, what a shame! I didn't see it before :P It was clear aahaha, thanks dude!
Victor Rodrigues
sometimes it happens, take a break :)
Canavar
I'm confused are you trying to show the proper code or just copying his code over again?
JoshBerke
yes, I copied the problematic part. it's obvious, no need to correct it I think.
Canavar
BTW this will work only in IE.
Crescent Fresh
@crescentfresh: what should I use to work on other browsers?
Victor Rodrigues
innerHTML will work on other browsers, but will include any HTML embedded within the label.
Josh Stodola
+5  A: 

In the second part of your code

var lblEmail = $get('<%= lblEmail.ClientID %>');
var email = lblEmail.innerText;

var lblUsername = '<%= lblUsername.ClientID %>';
var username = lblUsername.innerText;

$get call is absent. Should be

var lblEmail = $get('<%= lblEmail.ClientID %>');
var email = lblEmail.innerText;

var lblUsername = $get('<%= lblUsername.ClientID %>');
var username = lblUsername.innerText;
Alexander Prokofyev
A: 

What Alexander said. The javascript should be:

var lblEmail = $get('<%= lblEmail.ClientID %>'); var email = lblEmail.innerText;

var lblUsername = $get('<%= lblUsername.ClientID %>'); var username = lblUsername.innerText;

Jan