Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a field in SharePoint Display Form based on the field name (jQuery)?

I have a SharePoint list with the following single line of text fields: Title, Year and Type or Location. I want to be able to hide the Type or Location table row in the default display form. I know that I should create a JavaScript script and put it in Content Editor web part inside DispForm.aspx.

I am not fluent with jQuery syntax, thus I need help with the code, i.e. I don't know how to reference the table row which contains Type or Location field and its value. Here's what I've done so far, but it doesn't work:

jQuery(document).ready(function($) {
   $("input[title='Type or Location']").closest("tr").hide();
});

I know that the "input[title='Type or Location']" part is incorrect; at least I think it's that. Could anyone help me out? Thank you.

like image 803
Boris Avatar asked Dec 17 '22 02:12

Boris


2 Answers

Try:

jQuery(document).ready(function($) {
   $("h3.ms-standardheader:contains('Type or Location')").closest("tr").hide();
});
like image 117
Rich Bennema Avatar answered Dec 28 '22 23:12

Rich Bennema


I am not sure why you want to use jQuery for that. In SharePoint, you can choose to make a field required, optional or hidden. In most cases, just switching to hidden will address your issue.

For the record, I would also try to avoid as much as possible the use of jQuery(document).ready, it might conflict with the SharePoint out of the box onload event. In your case it is not needed.

Update: here is a way to do this with jQuery:

$("td.ms-formlabel:contains('Type or Location')").parent().hide();
like image 26
Christophe Avatar answered Dec 28 '22 23:12

Christophe