Ways to get sharepoint 2010 fields in jQuery: Editform vs Dispform

Hi again,
All about jQuery these days.This is such a powerful language to do a lot of cool things on SharePoint!
After my blogpost about hiding fields in the Editform in SharePoint, I got some questions on how to hide fields in the Dispform too.

So let us check out what happens in SharePoint and how you can use Jquery to hide these fields.

Editform / Newform

SharePoint output:

<tr> 
<td nowrap="true" valign="top" width="190px" class="ms-formlabel" > 
     <h3 class="ms-standardheader"> 
           <nobr>FIELD NAME</nobr> 
    </h3> 
</td > 
<td valign="top" class="ms-formbody"> 
   <!-- FieldName="FIELD NAME" FieldInternalName="FIELD INTERNAL NAME" FieldType="SPFieldText" --> 
   <span dir="none"> 
       <input name="Value$TextField" type="text" value="VALUE" maxlength="255" id="ID_TextField" title="FIELD TITLE" class="ms-long ms-spellcheck-true" />
       <br />
   </span>
</td> 
</tr> 

We are going to check the code until we find a NOBR tag that contains our value. Next we are going to the closest table row and hide that.
You can get the field name via the Nobr tag enclosing the field title:

$('nobr:contains("FIELD NAME")').closest('tr').hide();

Dispform

SharePoint output:

<TR> 
<TD nowrap="true" valign="top" width="165px" class="ms-formlabel"> 
 <H3 class="ms-standardheader"> 
      <a name="SPBookmark_FIELDNAME"> 
           </a>
               FIELD NAME 
 </H3> 
</TD> 
<TD valign="top" class="ms-formbody" width="450px" ID="SPFieldText"> 
 <!-- FieldName="FIELD NAME" FieldInternalName="FIELD NAME" FieldType="SPFieldText"  --> 
 FIELD VALUE; 
</TD> 
<TR> 

Here we have our Field name enclosed in the H3 tag.

$('h3:contains("FIELD NAME")').closest('tr').hide();

Or if you want to get the value of the field:

Var sField1 = $('h3:contains("Field1")').closest('td').next('td').text();

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.


2 thoughts on “Ways to get sharepoint 2010 fields in jQuery: Editform vs Dispform”

Leave a Reply