Solved

getElementById not working on master page

Posted on 2009-05-04
4
964 Views
Last Modified: 2012-05-06
I have a user control which has a document.getElementById call and it works on all of the pages I put the user control on except when I use the user control on a content page that uses a master page then I get the error document.getElementById is not a function?

 I don't have any spelling errors or anything like that because the control works fine when it is not on a master page page
This is the javascript function within the user control where the error is occuring on Master Page content pages only
 
    function ClearNoTab() {
        //alert('called');
        var noTabsDiv = document.getElementById('noTab');
        if (noTabsDiv != null) {
            noTabsDiv.style.display = "none";
        }
    }
 
  This is the actual object;
 
  <div id="noTab" style="display: none;">
 
Test<asp:Literal ID="litNominatedArticleNoTab" runat="server"></asp:Literal> </div>

Open in new window

0
Comment
Question by:AWSHelpdesk
  • 2
4 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24301068
Check to see if you have multiple document elements that have "noTab" as the id attribute.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24301082
wait a minute... I just re-read the statement again.  You get an error "document.getElementById" is not a function?  Wow, that's strange.

Can you provide the whole page?

Is the code where the document.getElementById() call exists with a <script> clause?

What does the <script> tag look like?

Does it look like?
<script type='text/javascript'>
 
or 
 
<script language='JavaScript'>

Open in new window

0
 
LVL 13

Expert Comment

by:drypz
ID: 24301317
Hi! By using MasterPages, the control names are renderd differently. The control id is prefixed by ctl01_ctl02...

Something like

     document.getElementById('ctl00_ContentPlaceHolder1_Control name').value

or you want to use this

     document.getElementById("<%=Controlname.ClientID %>").value
0
 
LVL 3

Accepted Solution

by:
RobbP earned 500 total points
ID: 24302655
Rather use jquery whereby you can directly call the control and its ID, otherwis - as stated above - you would need to reference the entire control ID. Problem with this is that the possibility is high that this could change on the fly.

So use the following:

JAVASCRIPT:
function ClearNoTab() {
        //alert('called');
        var noTabsDiv = $get('<%= noTab.ClientID %>');
        if (noTabsDiv != null) {
            noTabsDiv.style.display = "none";
        }
    }

MARKUP:
<asp:TextBox ID="noTab" runat="server"></asp:TextBox>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question