Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getElementById not working on master page

Posted on 2009-05-04
4
Medium Priority
?
991 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

610 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