jQuery solutions are so 2016... since IE9 everything can be done as easily with JavaScript
When you do not have to support pre IE9 browsers you can use vanilla JavaScript
You have to copy/paste 5 more lines of code compared to the jQuery approach,
but you do not have to worry about loading a 90KB jQuery library.
This only works on Classic Forms where the layout is a TABLE
Once the page is created you execute JavaScript to hide the TR displaying the Field
- Find Field by definition in (green)
<!-- comment -->
- 'walk up the DOM' to its TR
- Hide that TR
The native JavaScript TreeWalker API/class/Object can get the Comments node with one call.
(IE9 was the last Browser to finally support this)
One eval
statement can turn that comment into 3 JavaScript variables.
Solution
So the only native JavaScript you have to use
(Paste into the NewForm.aspx or in a Script OR Content Editor WebPart below your Form, so it executes after the Form is displayed)
is:
var formCSI = function (form){
function getComments(element) {
function acceptNode() { // IE *requires* this argument where other browsers don't.
return NodeFilter.FILTER_ACCEPT;
}
var safeFilter = acceptNode;// IE *requires* this argument where other browsers don't.
safeFilter.acceptNode = acceptNode;
return document.createTreeWalker(element, NodeFilter.SHOW_COMMENT, safeFilter, false);
}
var tree=getComments(form);
while (node=tree.nextNode()) {//loop all found <!-- comments -->
eval(node.nodeValue.replace(/ Field/g, ';var CSI_') + ';');//convert comment to vars
console.info(CSI_InternalName,CSI_Type,CSI_Name);
var TR=node;while (TR.tagName !== 'TR') TR = TR.parentNode;//find parent TR row
this[CSI_InternalName]=TR;// store TR as formCSI.[internalName] eg. formCSI.Title
}
return this;//return formCSI as object
}(document.querySelector('.ms-formtable'));//IIFE execute function immediatly
//formCSI now is an object referencing all Form Row TR elements
formCSI.Title.style.background='pink';
formCSI.AssignedTo.style.display='none';// 'table-row' to show again
This turns every Field in the Form into a JavaScript formCSI Object which you can use to color, hide, whatever you want to do with the TR rows
Also used in: