0

I am able to hide fields in the default new/edit forms using JS, but am having trouble sorting out how to do it when using customized new/edit forms with the hide/unhide function triggered by a checkbox in SP2013...

I cannot find a solid example of code to point me in the right direction; would anyone be able to assist?

Again, this is for >SP2010 using a checkbox as a trigger. I do have access to SPD, of course no InfoPath.

Thanks for your help.

2 Answers 2

0

New item entry form: how to hide 2 fields with a code in SP designer NewForm.aspx

Mentions SP2013 CSR code, but the Javascript (no need for jQuery) applies to SP2010 as well

Disclaimer: if you are just starting with JavaScript the code might seem complex, if you read the comments it should be clear

0

Here's an example in JQuery - we are on 2013 now, but I think this will work in 2010. You could also attach it to .change instead of .click. I tend to find the display text and then walk up to the row. Use the F12 developer tools and investigate the structure of the page with the DOM explorer - that will help you construct your jquery access.

   // on edit form, do your hide  based on current status to start things off.
// Use the names that show on the form.
// assume all are showing, and checking means to hide


if($('input[title="CHECKBOXDISPLAYNAME"]').is(':checked') ) {
           // hide rows
     $('nobr:contains("FIELD1DISPLAYNAME")').parents('tr:first').hide();

      $('nobr:contains("FIELD2DISPLAYNAME")').parents('tr:first').hide();
}  else {
      // do something if the checkbox is unchecked.
  }



// and then keep it in sync when the checkbox changes.        
        $('nobr:contains("CHECKBOXDISPLAYNAME")').parents('tr:first').find('input').click (function () {
            if ($(this).is(':checked')) {  
           // hide rows
           $('nobr:contains("FIELD1DISPLAYNAME")').parents('tr:first').hide();
           $('nobr:contains("FIELD2DISPLAYNAME")').parents('tr:first').hide();
            } else {
           // show rows
           $('nobr:contains("FIELD1DISPLAYNAME")').parents('tr:first').show();
           $('nobr:contains("FIELD2DISPLAYNAME")').parents('tr:first').show();
            }
         });

$(document).ready(function() { if($('input[title="Checkbox"]').is(':checked') ) { // hide rows $('nobr:contains("HideText")').parents('tr:first').hide(); } $('nobr:contains("Checkbox")').parents('tr:first').find('input').click (function () { if ($(this).is(':checked')) { // hide rows $('nobr:contains("HideText")').parents('tr:first').hide(); } else { // show rows $('nobr:contains("HideText")').parents('tr:first').show(); } }); });
8
  • Robin, with no fear of shame would you be able to give me more specific code? I'm new to this, and having difficulty following it through to its end. For instance, let's just say I have one yes/no checkbox that hides/unhides two items (both single lines of text). I'm not sure if the "do stuff if checked/unchecked" lines should refer to field titles, internal field names, etc.
    – Harry
    Commented Mar 30, 2017 at 19:53
  • The //do stuff is where you would hide or show your rows. For this type of code, use the display name that shows on the form itself. I'll update the above with more specifics Commented Mar 31, 2017 at 14:13
  • Robin, I tried to add the code I am using as is below yours. More or less, included the .js refs (which are working for me for other scripts) while changing the checkbox and item field names -- and placing it in the .aspx under Placeholder Main. Not seeing the action occur, and not sure why. Seems straightforward.
    – Harry
    Commented Mar 31, 2017 at 18:03
  • Not sure. I always add my code in a content editor webpart or html web part or script editor, I don't edit the .aspx directly. This code does need to run after the DOM is loaded, so it does have to be between: $(document).ready(function() { ....... }); statements. Are you getting any errors in your developer tools console tab? If you put an alert in the .click function does it alert you? Maybe try .change instead of .click? Commented Mar 31, 2017 at 19:23
  • I do appreciate your help, not sure where is failing - I'm sure it is something on my side, if the code is functioning for you. Bummer, as I really need it. I'm wrapping it in script tags, and have tried both adding it using a Content Editor and Script Editor, place below the form. No console errors other than an odd random syntax error. Should there be a .js link made from the form itself?
    – Harry
    Commented Apr 1, 2017 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.