0

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.

_spBodyOnLoadFunctionNames.push("hideFields");

//Sets Order By Display to True, Disables Priority Choices
function hideFields() {
   var control = findacontrol("Order ID");
   control.parentNode.parentNode.style.display="block";
   control = findacontrol("Priority");
   control.parentNode.parentNode.disabled="true";

  //call orderID
  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();
    setFocus();
    }

//Sets cursor Focus to NAPA users
function setFocus() {
    //set focus to NAPA user
    document.getElementById("ctl00_m_g_0537f4e9_69aa_409a_b5ed_15e3624efeab_ctl00_ctl05_ctl03_ctl00_ctl00_ctl04_ctl00_ctl00_UserField_upLevelDiv").focus();
}

//end custom functionality-->
 </script>
  • 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 – Robert Lindgren Aug 13 '13 at 19:41
  • Everything works fine in IE though, I believe the code fails in Firefox and Chrome. – Batman Aug 13 '13 at 19:50
  • Why not find the source of the error in Firebug? – RJ Cuthbertson Aug 13 '13 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 Aug 13 '13 at 20:09
  • My comment about using jQuery for finding elements and their parents is still valid though – Robert Lindgren Aug 14 '13 at 6:23
3

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 Aug 14 '13 at 13:58
  • Well, "what is it actually?" is a question to you: What do you want to find with "document.getElementsByTagName("!");"? – PeterB Aug 14 '13 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 Aug 14 '13 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 Aug 14 '13 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 Aug 14 '13 at 15:40
0

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++)  ...
 children[i]
  • I;m not really sure how to do this or what to change. – Batman Aug 13 '13 at 20:41
  • when you say getelementbyid(parent of comments) are you talking about the form as being the parent? – Batman Aug 15 '13 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 Aug 15 '13 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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