I have this JavaScript code for my SharePoint forms which does not work in other browsers but Internet Explorer.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script><script type="text/javascript">
// This is custom functionality that hides selected columns in the a list.


//Sets Order By Display to True, Disables Priority Choices
function hideFields() {
   var control = findacontrol("Order ID");
   control = findacontrol("Priority");

  //call orderID

function findacontrol(FieldName) {
   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }

//Sets Order ID field to read only in edit form
function orderID() {
    //Set the Field to Read only and change its background colour
        $("input[title='Order ID']").attr("readonly","true").css('background-color','#F6F6F6');

    //call setFocus();

//Sets cursor Focus to NAPA users
function setFocus() {
    //set focus to NAPA user

//end custom functionality-->
  • 1
    I have had problems with getElementsByTagName in IE in the past, that could be your problem. Since I see you are loading jQuery, use jQuery selectors instead Commented Aug 13, 2013 at 19:41
  • Everything works fine in IE though, I believe the code fails in Firefox and Chrome.
    – Batman
    Commented Aug 13, 2013 at 19:50
  • Why not find the source of the error in Firebug? Commented Aug 13, 2013 at 20:04
  • I looked at the Chrome console and it says "Cannot read property 'parentNode' of undefined" but this doesn't happen in IE. I've checked parentNode browser support and Chrome and Firefox support it so i'm not sure how to go about fixing it.
    – Batman
    Commented Aug 13, 2013 at 20:09
  • My comment about using jQuery for finding elements and their parents is still valid though Commented Aug 14, 2013 at 6:23

2 Answers 2


I think there are two slightly dangerous things in your code, that could be the source of the problem:

a) document.getElementsByTagName("!"); Document.getElementsByTagName() is meant for getting a collection of HTML tags, e.g. getElementsByTagName("table") will give you all tables As "!" is not a HTML tag to be used for standard page content, just for some declarative directives like "" and comments "". It seems like your looking for sth. different (what is it actually?), so this could be the source of a problem. ... and what ever you're looking for needs to be still present in the HTML that arrives on the client side, of course.

b) With control.parentNode.parentNode. ... your expecting, that whatever result you get from findacontrol has at least grandparents. If not you're running into an error which is not caught. You might want to check if the necessary conditions are met before accessing an object, or at least (always) catch possible errors and make them appear in an error message.

So much for the specific parts. In general: I agree with Mike (see his answer), that getting a component by its ID, i.e. via document.getElementById() is typically preferrable, as you know exactly what you get.

  • Sorry, I don't understand what you're saying here: "It seems like your looking for sth. different (what is it actually?)"
    – Batman
    Commented Aug 14, 2013 at 13:58
  • Well, "what is it actually?" is a question to you: What do you want to find with "document.getElementsByTagName("!");"?
    – PeterB
    Commented Aug 14, 2013 at 15:01
  • Oh, I'm trying to find controls in my Edit Form. I've been using this code to either hide or disable certain fields within forms. The ID's for those fields are really screwed up and I ended up find this code which seemed pretty simple to use but won't work in anything else other than IE
    – Batman
    Commented Aug 14, 2013 at 15:09
  • :-) ... which controls? the tags of controls typically don't start with '<!'. Example: If you want to look for all textfields, you could do 'document.getElementsByTagName("input");' and filter for 'type="text"' and probably some pattern in 'id'.
    – PeterB
    Commented Aug 14, 2013 at 15:19
  • The code above searches for "Order ID" which is a text field and "Priority" which is a Choice Field using radiobox style. I liked using the code above because all I had to do was but in the control's name and it would find it and apply the changes. Using the controls ID is challenging in SP because I can't set it myself and it renders a ridiculous value. This is where I found the code before adding to it: intrateam.com/gb/artikel/…
    – Batman
    Commented Aug 14, 2013 at 15:40

If you can, use var pn=getElementById('parent_of_comments'), then enumerate it's children like:

var children = pn.childNodes;  
for (var i = 0; i < children.length;
i++)  ...
  • I;m not really sure how to do this or what to change.
    – Batman
    Commented Aug 13, 2013 at 20:41
  • when you say getelementbyid(parent of comments) are you talking about the form as being the parent?
    – Batman
    Commented Aug 15, 2013 at 5:26
  • I don't understand your solution or how to implement it. From what i gather "document.getElementsByTagName("!")" is likely giving me an error right? What if I used something more restrictive like getelementbyattribute('title') then remunerate through that to find Order Id or Priority, then try doing the parentNode.parentNode...would that work?
    – Batman
    Commented Aug 15, 2013 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.