Skip to main content
Inspiring
Jan 9, 2017 3:17 PM UTC
Answered

Can't delete all section

Hi guys,

Why does ID not allow you to delete all sections.

for ( i = allSections.length; i > 0; i-- ) {

    allSections.remove();

    }

This says undefined is not an object but I have 2 sections in my document.

allSections = doc.sections.everyItem().getElements();

This topic is closed to new replies. Start a new post to keep the conversation going.
Peter Kahrel
Peter KahrelCorrect answer
Community Expert
Jan 9, 2017 3:40 PM UTC

A document must have at least one section with at least one page.

P.

9 replies

Jongware
Community Expert
Community Expert
Jan 9, 2017 8:13 PM UTC

While Peter is right (naturally), it's not the reason why you get that error.

If you have 2 sections, the count is '2' but since counting starts at 0, their indexes are 0 and 1.

You try to remove a section with index 2, which does not exist. So you get that error.

Translate
Peter Kahrel
Community Expert
Community Expert
Jan 9, 2017 8:34 PM UTC

> You try to remove a section with index 2, which does not exist. So you get that error.

I read the first sentence and thought that I had seen enough. Clearly not!

Translate
Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
Jan 9, 2017 3:40 PM UTC

A document must have at least one section with at least one page.

P.

Translate
Obi-wan Kenobi
Legend
Jan 9, 2017 8:26 PM UTC

Hi,

Are these 3 codes equivalent?

Thanks in advance!

app.activeDocument.sections.itemByRange(1, app.activeDocument.sections.length-1).remove();

allSections = app.activeDocument.sections.everyItem().getElements();

S = allSections.length;

for ( var s = 1 ; s < S ; s++)  allSections.remove();

allSections = app.activeDocument.sections.everyItem().getElements();

for ( s = allSections.length-1 ; s > 0 ; s-- )  allSections.remove();

(^/)

Translate
Peter Kahrel
Community Expert
Community Expert
Jan 9, 2017 8:36 PM UTC

Well, Michel, they have the same result so in that sense they're equivalent. The approaches are slightly different. So it depends on what you understand by 'equivalent'.

P.

Translate