Document.ready() not working in list new form SharePoint 2013

Document.ready() not working in list new form-2013

Scenario:
User want to hide a field in new form without custom c# code. I have tried with jquery and was trying to hide the field row in newform.aspx. But document.ready() is firing in newform.aspx.

Code snippet I was trying to use is:

<script language=”javascript” type=”text/javascript”>
$(document).ready(function() {
$(“input[title*=’FirstName’]”).val(“Krishna.Vandanapu”);
var field = $(“input[title*=’FirstName’]”); // The * in title is needed because SharePoint 2013 uses “Required Field” on required fields.
field.parent().parent().parent().hide();
});
</script>

In the above code block I was trying to hide the row of FirstName field in the below screen with a default value as “Krishna.Vandanapu”. Since it is a mandatory field I set the default value.

Document.ready() not working in list new form-2013
Document.ready() not working in list new form-2013

The issue with the above code is I was missing the jquery reference library inclusion.

Solution:
We have to add the ajax library file http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js in the script to fire the $(document).ready function.

The final code will be as follows:

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>
<script language=”javascript” type=”text/javascript”>
$(document).ready(function() {
alert(“Krishna”);
$(“input[title*=Title]”).val( “Title Value-1” );
$(“input[title*=’FirstName’]”).val(“Krishna.Vandanapu”); //The * in title is needed because SharePoint 2013 uses “Required Field” on required fields.
var field = $(“input[title*=’FirstName’]”);
field.parent().parent().parent().hide();
});
</script>


The output will be as follows:

Document.ready() not working in list new form online
Document.ready() not working in list new form online

Hope it helps !!!!


You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →