Hi,

I'm playing with javaScript and AJAX and using the source code used in this page: (Actually I copied the code on the Default.aspx to the page I was working on, which has a MasterPage and translated from C# to VB.NET)
However, I'm having an "Object Required Error" in the line

divRef.style.display = "block";

Any ideas why is this happening?

Thanks,

Ana

Dani AI

Generated

Brief summary: the IE "Object required" error means your script tried to use a DOM object that wasn't there (null). In this thread correctly pointed out the element couldn't be found and solved it by using the control's rendered client id. Two common causes are (a) the server control's client-side id is different because of ASP.NET naming containers (master pages, user controls), and (b) the script runs before the element exists in the DOM.

Practical fixes and gotchas

  • Verify the rendered id with "View Source" or the browser inspector. ASP.NET often prefixes ids; the literal server ID is not always the client id.
  • If you can use ASP.NET 4+, make the control emit a stable id so client code can reference it predictably:
    <asp:Panel ID="divCheckBoxList" runat="server" ClientIDMode="Static">...</asp:Panel>
  • If you must use the server-generated id, emit it into page script (server-side expression) and then pass that value to your JS. Example pattern:
    <script>
    var cbListClientId = '<%= divCheckBoxList.ClientID %>';
    // call your JS using that variable
    </script>

    Note: server-side expressions like the above only work in inline page markup, not in an external .js file. For external scripts emit a small inline initializer (or a data- attribute) and then use that value.

Other tips

  • Ensure the script runs after the DOM element exists: put the script after the element, use DOMContentLoaded/window.onload, or register startup scripts correctly (ScriptManager.RegisterStartupScript for UpdatePanel scenarios).
  • Debug with browser dev tools: try the id in the console, inspect the DOM, and use console.log to confirm getElementById returns an element.
  • Cross-browser: prefer textContent over innerText for standards compliance, and be careful with selector escaping if using jQuery and unusual ids.

This covers the root causes and safe fixes so the code will reliably find the element regardless of master pages, partial postbacks, or external scripts.

Recommended Answers

All 4 Replies

divRef probably doesn't exist, show the whole code not just one line.

The code was on the link. But here it goes:

<script type="text/javascript">
   var timoutID;

   //This function shows the checkboxlist
   function ShowMList()
   {
       var divRef = document.getElementById("divCheckBoxList");
       [B]divRef.style.display = "block";[/B]
       var divRefC = document.getElementById("divCheckBoxListClose");
       divRefC.style.display = "block";
   }

   //This function hides the checkboxlist
   function HideMList()
   {
       document.getElementById("divCheckBoxList").style.display = "none";
       document.getElementById("divCheckBoxListClose").style.display = "none";
   }

   //This function finds the checkboxes selected in the list and using them,
   //it shows the selected items text in the textbox (comma separated)
   function FindSelectedItems(sender,textBoxID)
   {
       var cblstTable = document.getElementById(sender.id);
       var checkBoxPrefix = sender.id + "_";
       var noOfOptions = cblstTable.rows.length;
       var selectedText = "";
       for(i=0; i < noOfOptions ; ++i)
       {
          if(document.getElementById(checkBoxPrefix+i).checked)
          {
             if(selectedText == "")
                selectedText = document.getElementById
                                   (checkBoxPrefix+i).parentNode.innerText;
             else
                selectedText = selectedText + "," +
                 document.getElementById(checkBoxPrefix+i).parentNode.innerText;
          }
       }
       document.getElementById(textBoxID.id).value = selectedText;
    }
</script>

well there's your answer:

document.getElementById("divCheckBoxList");

Isn't returning anything, it can't find that element. My first tip is to use Firefox and Firebug so you can actually see the error since Internet Explorer has pretty much every JS error as Object Expected because everything in JS is an object

You are right, ShawnCplus!
The correct form would be: document.getElementById(<%= myControl.ClientID %>')

Thanks =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.