Solved

getElementById not working on master page

Posted on 2009-05-04
4
974 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

717 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