Removing errant spacers

On another thread (Spacer with classes "spacer gfield" - why is it appearing?) there is a script which can be run to remove errant spacers from the form layout and on that page we are instructed to

enter the field ID of the field which precedes the spacer

I’ve used this script before with varying success but the bit which always trips me up is what you determine to the the “field ID”. Do you mean simply the number of the field, displayed at the top of the field properties when it is selected (ie 15), the one displayed on the frontend code (ie field_93_15) or the one displayed on the backend code (ie field_15)?

It would be great to have clarity on this.

When using that script, you can add simply the digit(s) of the field ID. So, to remove a spacer after Field ID 15, enter 15 when prompted. Here is a modified script that will let you enter multiple field IDs (comma-separated), if you need to remove a number of spacers from the same form.

var removeOrphanedSpacersAfter = window.prompt('Remove orphaned spacer after which field ID(s)?').split(/[ ,]+/);

for ( let i = 0; i < form.fields.length; i++ ) {
    if ( removeOrphanedSpacersAfter.includes( form.fields[i].id.toString() ) ) {
        let orphanedSpacer = document.getElementById(`field_${form.fields[i].id}`).nextElementSibling;
        if ( orphanedSpacer.classList.contains('spacer') ) {
            form.fields[i].layoutSpacerGridColumnSpan = 0;
            orphanedSpacer.remove();
        }
    }
}

Perfect, thanks Joshua, and also great to have the ability to add multiple fields at once.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.