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 !!!!

Similar SharePoint 2013 Tutorials


Leave a Reply