Solved

getElementById not working on master page

Posted on 2009-05-04
4
955 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
Comment Utility
Check to see if you have multiple document elements that have "noTab" as the id attribute.
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now