But when used in, aspx page that uses master page it gives above error.
This is because the rendered 'ID' of the controls in master page case is not exactly the same as you have defined. Controls are merged into master page template and the names are changed (a fixed master page naming convention string is appended to the id provided by you)
Two options:
1. Do a ViewSource, find the ID that is being defined for it (this will be same on every render of the page once page design is done). Use this ID to
getElementById
2. Use server tag delimiter. Refer:
Server side Delimiters in ASP.NET[
^]. Use
<%= %>
to get the server control's ID
NOTE: If the control is a HTML control (not a server control, without runat="server" attribute) then there is no such problem. ID's get changed at the time of rendering only for server controls.
UPDATE:
You can also use jQuery toggle for such functionality:
.toggle()[
^]