getElementById returning null value

R

Rob

Hi,

I have a label that I'm trying to make invisible on the client side using
the OnClientClick event for a button.

In the javascript function I have the following code:

var x = getElementById("ctl00_ContentPlaceHolder1_label");
x.Visible = true;

The function is being called correctly, but I can't get getElementId to
return anything other than NULL. We're using AJAX and I've tried calling the
script using label.ClientID from the server side using RegisterStartupScript
in the Page Load event on the server side. But the result is the same. I
checked the ClientID and it's definitely ctl00_ContentPlaceHolder1_label

I've also tried using get$ in place of getElementById, but the result is the
same.

It's not finding ctl00_ContentPlaceHolder1_label, but I don't know what else
it could be. Any advice would be appreciated.

Regards,
 
G

grava

Rob said:
Hi,

I have a label that I'm trying to make invisible on the client side using
the OnClientClick event for a button.

In the javascript function I have the following code:

var x = getElementById("ctl00_ContentPlaceHolder1_label");
x.Visible = true;

The function is being called correctly, but I can't get getElementId to
return anything other than NULL. We're using AJAX and I've tried calling
the script using label.ClientID from the server side using
RegisterStartupScript in the Page Load event on the server side. But the
result is the same. I checked the ClientID and it's definitely
ctl00_ContentPlaceHolder1_label

I've also tried using get$ in place of getElementById, but the result is
the same.

It's not finding ctl00_ContentPlaceHolder1_label, but I don't know what
else it could be. Any advice would be appreciated.

Regards,

Have you inspected your HTML Source produced by your app ? Is the
"ctl00_ContentPlaceHolder1_label" element present ?
 
R

Rob

Hi,

I've checked in the source and the element is not present. I have it
initially set to invisible. I tried setting it to visible intially and it
was in the source. If an invisible element does not appear in the source, I
don't know how I'm supposed to toggle it between visible and invisible on
the client side

Thanks.
 
E

Eliyahu Goldin

Two things:

1. var x = getElementById("<%= label.ClientID %>"); looks much better

2. Likely the label has it's Visible property set to false/ If so, it is not
rendered to the client in the first place. Consider hiding with css rule
display:none.


--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
P

Patrice

The Visible property is a server side property so. It it is false the
element is not rendered at all in the HTML page.

If you want to toggle this client side you could use a style (such as
visibility:hidden or display:none) and toggle this style value client
side...

--
Patrice

Rob said:
Hi,

I've checked in the source and the element is not present. I have it
initially set to invisible. I tried setting it to visible intially and it
was in the source. If an invisible element does not appear in the source,
I don't know how I'm supposed to toggle it between visible and invisible
on the client side

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top