This project has moved and is read-only. For the latest updates, please go here.

Can't get sample working

Jan 22, 2015 at 12:00 AM
Hi there,

I've spent a couple of hours on this but still can't get the sample working despite attempting multiple variations of code. Originally I was attempting to make the show/hide field work, but when that failed I went back to attempting the basic hello world example.

Any support is appreciated!

I used "edit source" within the content editor rather than link to the html page containing the code - however I did attempt that process also and it failed.
<script src="/sites/testing/SiteAssets/jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="/sites/testing/SiteAssets/sputility.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
SPUtility.GetSPFieldByInternalName('Title').SetValue('Hello world!');
});
</script>
Thank you
Jan 25, 2015 at 7:09 PM
Jacro,
I'll try to help, can you give me some more information? What version of SharePoint are you using? Are you trying to add the code to newform or editform?

First, make sure you've uploaded jQuery and SPUtility to a library in your SharePoint site. Installation instructions

Second, download gettingstarted.html. Update the "src" attribute for the jQuery and SPUtility scripts.

Third, upload this HTML page to the same Document library.

You should expect to see output like this:

Getting started
Thanks,
Kit
Jan 27, 2015 at 11:54 PM
Hi Kit,

Thanks for getting back to me.

My organisation is using SharePoint 2013, and I have tried adding code to both New and Edit forms.
Separate to your efforts with SPUtility, I was able to get CascadingDropdowns_v3 from spjsblog.com to work if that potentially helps in troubleshooting.

I have followed your instructions and tested using gettingstarted.html.

Image

It seems to load jQuery no problems, but I receive an error that SPUtility cannot be found.

Image
Jan 28, 2015 at 3:28 AM
Hm.. thats strange. Definitely looks like SPUtility is not being loaded for some reason. Maybe a dumb question but you uploaded sputility.js instead of sputility.min.js? Are you getting any other errors?

If you have any other details about your setup I can try to reproduce your setup... browser, list or library, fields, etc.
Jan 28, 2015 at 3:48 AM
Hi Kit,

All good now - not sure what happened when I originally downloaded the sputility source - just downloaded the file a second time and it worked :)
Jan 28, 2015 at 3:51 AM
For reference, I backtracked and realised I'd downloaded this page thinking it was the JS file.
Jan 28, 2015 at 4:44 AM
Me again :/

So the first part of this code works - it hides the 'City' field if my default value is "other", however the hide/show based on new selected value doesn't seem to work for me.. Any ideas? Again - using SP 2013. (i'm using the code you link to - I'd adjusted the SPUtility.GetSPField('Country').Hide() to "HideSPField" as a test - same results.

I'm a total Javascript newbie trying to just get by customising other peoples examples if you couldn't already tell....
<script src="/sites/BPOMgmt/OMS/SiteAssets/jquery-1.11.2.min.js" type="text/javascript"></script><script src="/sites/BPOMgmt/OMS/SiteAssets/sputility.min.js" type="text/javascript"></script><script type="text/javascript">
$(document).ready(function(){

    // -----  Hide the field at the begin if the default value is other.
    // Get a single select dropdown field
    var countryField = SPUtility.GetSPField('Country');

    var countryFieldValue = countryField.GetValue();

    // Hide the City field if the selected value is Other
    if(countryFieldValue == 'Other') {
        SPUtility.HideSPField('City');
    }
    else {
        SPUtility.ShowSPField('City');
    }

    // -----  Hide/Show the field based on new selected value.
    var id = SPUtility.GetSPField('Country').Dropdown.id;

    $('#'+id).on('change', function() {
        if (countryFieldValue == 'Other') {
            SPUtility.HideSPField('City');
        }
        else {
            SPUtility.ShowSPField('City');
        }
    });


});
</script> 
Jan 28, 2015 at 1:37 PM
Edited Jan 28, 2015 at 1:37 PM
Woohoo! I'm glad you were able to figure out the file download. Was there anything in the documentation I should change?

The problem above is you never get the new value after the user changes the dropdown. Try this code:
<script src="/sites/BPOMgmt/OMS/SiteAssets/jquery-1.11.2.min.js" type="text/javascript"></script><script src="/sites/BPOMgmt/OMS/SiteAssets/sputility.min.js" type="text/javascript"></script><script type="text/javascript">
// wait for the window to load
$(document).ready(function () {
    // Get a single select dropdown field
    var countryField = SPUtility.GetSPField('Country');

    // create a function to show or hide City based on Country's value
    var showOrHideField = function() {
        var countryFieldValue = countryField.GetValue();
        // Hide the City field if the selected value is Other
        if(countryFieldValue === 'Other') {
            SPUtility.HideSPField('City');
        }
        else {
            SPUtility.ShowSPField('City');
        }
    };
    
    // run at startup (for edit form)
    showOrHideField();

    // make sure if the user changes the value we handle it
    $(countryField.Dropdown).on('change', showOrHideField);
});
</script> 
Jan 29, 2015 at 12:35 AM
Hi Kit,

Excellent! I have customised it for my form and it's working perfectly, thanks so much for taking the time to help!

In regards to documentation - your own documentation is great. The problem I had was that I initially found your JS via this page: http://sharepoint.stackexchange.com/questions/88064/hide-show-field-on-list-form-based-on-value-from-another-field

I clicked on the SPutility.js link there, then clicked the "Dist" think, then mistakenly right clicked "sputility.min.js" there and saved that link. I just didn't realise I had to get the raw file. I guess there's the potential that others with my lack of experience may also do this. I now know for next time though!
Jan 29, 2015 at 3:47 PM
Awesome, I'm glad you got it working and thanks for the feedback.