Custom Javascript on form
Hi All, Firstly - I know my javascript/jQuery is pretty terrible - please feel free to abuse me and give me examples of what I need to do better :) I am trying to use javascript to hide the Name field on a list form if the content type is of a specific type (I will set the Name value in the PreSaveAction when save is clicked). However when my $(document).ready function runs to do the check and hide the field, it runs before the List web part has added the field control objects to the DOM. I have tried using $(window).load as well. The closest I have come to making it work the way I want is to get the SELECT ctrl for the Content Type, and then override its onchange value. However then the function ChangeContentType is undefined: jQuery(document).ready(function() { var ctSelect = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select'); alert(Start); ctSelect.bind(onchange, function() { ChangeContentType(ctSelect.attr('id')); /* THIS IS UNDEFINED */ var contentTypeRow = jQuery('table#formTbl tbody tr:nth-child(1)'); var nameRow = jQuery('nobr:contains(Name)').closest(tr) var ctSelectVal = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select option:selected'); alert(ctSelectVal.text()); // If this is Lease Matrix Document Set if (ctSelectVal.text() == Lease Matrix Document Set) {contentTypeRow.hide(); nameRow.hide(); } return false; });}); Can someone point me in the right direction? Cheers, Nigel ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
Re: Custom Javascript on form
the document ready event will fire when the DOM is ready, however if you have external script libraries which perform asynchronous changes to the DOM, then this event is rendered nearly useless. You need to ensure that your script is fired on the result of that asynchronous change. In your case, I'm tipping it's probably SP.JS. To do this, you can use SP.SOD.executeOrDelayUntilScriptLoaded with SP.js as the target. On Fri, Oct 18, 2013 at 12:12 PM, Nigel Witherdin nigel_wither...@hotmail.com wrote: Hi All, Firstly - I know my javascript/jQuery is pretty terrible - please feel free to abuse me and give me examples of what I need to do better :) I am trying to use javascript to hide the Name field on a list form if the content type is of a specific type (I will set the Name value in the PreSaveAction when save is clicked). However when my $(document).ready function runs to do the check and hide the field, it runs before the List web part has added the field control objects to the DOM. I have tried using $(window).load as well. The closest I have come to making it work the way I want is to get the SELECT ctrl for the Content Type, and then override its onchange value. However then the function ChangeContentType is undefined: jQuery(document).ready(function() { var ctSelect = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select'); alert(Start); ctSelect.bind(onchange, function() { ChangeContentType(ctSelect.attr('id')); /* THIS IS UNDEFINED */ var contentTypeRow = jQuery('table#formTbl tbody tr:nth-child(1)'); var nameRow = jQuery('nobr:contains(Name)').closest(tr) var ctSelectVal = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select option:selected'); alert(ctSelectVal.text()); // If this is Lease Matrix Document Set if (ctSelectVal.text() == Lease Matrix Document Set) { contentTypeRow.hide(); nameRow.hide(); } return false; }); }); Can someone point me in the right direction? Cheers, Nigel ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
Re: Custom Javascript on form
Another alternative is to use the _spBodyOnLoadFunctionNames array. See: http://www.stephanrocks.com/2011/10/05/_spbodyonloadfunctionnames-in-sharepoint-vs-jquerys-document-ready/ Also if you are using SharePoint 2013, the JSLink functionality can accomplish the same thing as what you are trying to achieve, but is much cleaner. http://www.martinhatch.com/2013/08/jslink-and-display-templates-part-1-overview-url-tokens-and-applying-jslink-to-objects.html Mark From: Matthew Cosier cos...@gmail.commailto:cos...@gmail.com Reply-To: ozMOSS ozmoss@ozmoss.commailto:ozmoss@ozmoss.com Date: Friday, 18 October 2013 11:22 AM To: ozMOSS ozmoss@ozmoss.commailto:ozmoss@ozmoss.com Subject: Re: Custom Javascript on form the document ready event will fire when the DOM is ready, however if you have external script libraries which perform asynchronous changes to the DOM, then this event is rendered nearly useless. You need to ensure that your script is fired on the result of that asynchronous change. In your case, I'm tipping it's probably SP.JS. To do this, you can use SP.SOD.executeOrDelayUntilScriptLoaded with SP.js as the target. On Fri, Oct 18, 2013 at 12:12 PM, Nigel Witherdin nigel_wither...@hotmail.commailto:nigel_wither...@hotmail.com wrote: Hi All, Firstly - I know my javascript/jQuery is pretty terrible - please feel free to abuse me and give me examples of what I need to do better :) I am trying to use javascript to hide the Name field on a list form if the content type is of a specific type (I will set the Name value in the PreSaveAction when save is clicked). However when my $(document).ready function runs to do the check and hide the field, it runs before the List web part has added the field control objects to the DOM. I have tried using $(window).load as well. The closest I have come to making it work the way I want is to get the SELECT ctrl for the Content Type, and then override its onchange value. However then the function ChangeContentType is undefined: jQuery(document).ready(function() { var ctSelect = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select'); alert(Start); ctSelect.bind(onchange, function() { ChangeContentType(ctSelect.attr('id')); /* THIS IS UNDEFINED */ var contentTypeRow = jQuery('table#formTbl tbody tr:nth-child(1)'); var nameRow = jQuery('nobr:contains(Name)').closest(tr) var ctSelectVal = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select option:selected'); alert(ctSelectVal.text()); // If this is Lease Matrix Document Set if (ctSelectVal.text() == Lease Matrix Document Set) { contentTypeRow.hide(); nameRow.hide(); } return false; }); }); Can someone point me in the right direction? Cheers, Nigel ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: Custom Javascript on form
Thanks!! Im using SP2010 and the trick of pushing it into _spBodyOnLoadFunctionNames to make it run at the end of the other scripts was just what I was after! Cheers, Nigel From: mda...@datacogs.com To: ozmoss@ozmoss.com Date: Thu, 17 Oct 2013 21:27:22 -0400 Subject: Re: Custom Javascript on form Another alternative is to use the _spBodyOnLoadFunctionNames array. See: http://www.stephanrocks.com/2011/10/05/_spbodyonloadfunctionnames-in-sharepoint-vs-jquerys-document-ready/ Also if you are using SharePoint 2013, the JSLink functionality can accomplish the same thing as what you are trying to achieve, but is much cleaner. http://www.martinhatch.com/2013/08/jslink-and-display-templates-part-1-overview-url-tokens-and-applying-jslink-to-objects.html MarkFrom: Matthew Cosier cos...@gmail.com Reply-To: ozMOSS ozmoss@ozmoss.com Date: Friday, 18 October 2013 11:22 AM To: ozMOSS ozmoss@ozmoss.com Subject: Re: Custom Javascript on form the document ready event will fire when the DOM is ready, however if you have external script libraries which perform asynchronous changes to the DOM, then this event is rendered nearly useless. You need to ensure that your script is fired on the result of that asynchronous change. In your case, I'm tipping it's probably SP.JS. To do this, you can use SP.SOD.executeOrDelayUntilScriptLoaded with SP.js as the target. On Fri, Oct 18, 2013 at 12:12 PM, Nigel Witherdin nigel_wither...@hotmail.com wrote: Hi All, Firstly - I know my javascript/jQuery is pretty terrible - please feel free to abuse me and give me examples of what I need to do better :) I am trying to use javascript to hide the Name field on a list form if the content type is of a specific type (I will set the Name value in the PreSaveAction when save is clicked). However when my $(document).ready function runs to do the check and hide the field, it runs before the List web part has added the field control objects to the DOM. I have tried using $(window).load as well. The closest I have come to making it work the way I want is to get the SELECT ctrl for the Content Type, and then override its onchange value. However then the function ChangeContentType is undefined: jQuery(document).ready(function() {var ctSelect = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select');alert(Start);ctSelect.bind(onchange, function() {ChangeContentType(ctSelect.attr('id')); /* THIS IS UNDEFINED */ var contentTypeRow = jQuery('table#formTbl tbody tr:nth-child(1)');var nameRow = jQuery('nobr:contains(Name)').closest(tr)var ctSelectVal = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select option:selected');alert(ctSelectVal.text());// If this is Lease Matrix Document Setif (ctSelectVal.text() == Lease Matrix Document Set) {contentTypeRow.hide();nameRow.hide();}return false;});}); Can someone point me in the right direction? Cheers, Nigel ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
Re: Custom Javascript on form
Did that resolve your problem, Nigel? 2010 a swathe of scripts that get loaded in the same manner and the final rendering of list views and forms happens very late in the piece. I had a lot of legacy jquery that broke after upgrading and the push method only resolved a few. The rest required a more brute force approach. If you're familiar with the HTML Calculated Column from the the Path To SharePoint blog, you can find a variety of solutions in his four part series for 2010. http://blog.pathtosharepoint.com/2010/06/16/html-calculated-column-solutions-for-sp-2010-part-iv/ Regards, Paul On 18 October 2013 12:45, Nigel Witherdin nigel_wither...@hotmail.comwrote: Thanks!! Im using SP2010 and the trick of pushing it into _spBodyOnLoadFunctionNames to make it run at the end of the other scripts was just what I was after! Cheers, Nigel -- From: mda...@datacogs.com To: ozmoss@ozmoss.com Date: Thu, 17 Oct 2013 21:27:22 -0400 Subject: Re: Custom Javascript on form Another alternative is to use the _spBodyOnLoadFunctionNames array. See: http://www.stephanrocks.com/2011/10/05/_spbodyonloadfunctionnames-in-sharepoint-vs-jquerys-document-ready/ Also if you are using SharePoint 2013, the JSLink functionality can accomplish the same thing as what you are trying to achieve, but is much cleaner. http://www.martinhatch.com/2013/08/jslink-and-display-templates-part-1-overview-url-tokens-and-applying-jslink-to-objects.html Mark From: Matthew Cosier cos...@gmail.com Reply-To: ozMOSS ozmoss@ozmoss.com Date: Friday, 18 October 2013 11:22 AM To: ozMOSS ozmoss@ozmoss.com Subject: Re: Custom Javascript on form the document ready event will fire when the DOM is ready, however if you have external script libraries which perform asynchronous changes to the DOM, then this event is rendered nearly useless. You need to ensure that your script is fired on the result of that asynchronous change. In your case, I'm tipping it's probably SP.JS. To do this, you can use SP.SOD.executeOrDelayUntilScriptLoaded with SP.js as the target. On Fri, Oct 18, 2013 at 12:12 PM, Nigel Witherdin nigel_wither...@hotmail.com wrote: Hi All, Firstly - I know my javascript/jQuery is pretty terrible - please feel free to abuse me and give me examples of what I need to do better :) I am trying to use javascript to hide the Name field on a list form if the content type is of a specific type (I will set the Name value in the PreSaveAction when save is clicked). However when my $(document).ready function runs to do the check and hide the field, it runs before the List web part has added the field control objects to the DOM. I have tried using $(window).load as well. The closest I have come to making it work the way I want is to get the SELECT ctrl for the Content Type, and then override its onchange value. However then the function ChangeContentType is undefined: jQuery(document).ready(function() { var ctSelect = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select'); alert(Start); ctSelect.bind(onchange, function() { ChangeContentType(ctSelect.attr('id')); /* THIS IS UNDEFINED */ var contentTypeRow = jQuery('table#formTbl tbody tr:nth-child(1)'); var nameRow = jQuery('nobr:contains(Name)').closest(tr) var ctSelectVal = jQuery('table#formTbl tbody tr:nth-child(1) td.ms-formbody select option:selected'); alert(ctSelectVal.text()); // If this is Lease Matrix Document Set if (ctSelectVal.text() == Lease Matrix Document Set) { contentTypeRow.hide(); nameRow.hide(); } return false; }); }); Can someone point me in the right direction? Cheers, Nigel ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss