Solved

getElementById not working on master page

Posted on 2009-05-04
4
959 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 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

26 Experts available now in Live!

Get 1:1 Help Now