Skip to main content
dublove
Legend
April 12, 2026
Answered

How to move multiple selected objects to the top layer of all objects?

  • April 12, 2026
  • 2 replies
  • 39 views

Suppose I’ve selected three objects. In the end, I want those three objects to remain selected and be moved to the top of the stack relative to all other objects.

I tried the code below, but it seems to only support a single object.
Of course, only one can be at the very top.
I just need the selected objects to be on top of the others. 

  var d = app.activeDocument;
    var item = d.selection[0];
    var items = d.selection; 
    var sel = items;

//do something

      d.selection = sel;
        for (j = 0; i < sel .length; j++) {
            //sel[j].bringToFront();
sel[j].bringForward();
        }

 

    Correct answer rob day

    Hi ​@dublove , Also, you are mixing j and i variables in you example, so the loop as you have written it doesn’t work. Here’s ​@m1b ’s code including the for statement and consistent variables:

     

    var d = app.activeDocument;
    var sel = d.selection;
    var topLayer = d.layers.firstItem();
    for (i = 0; i < sel .length; i++) {
    sel[i].itemLayer = topLayer;
    sel[i].bringToFront();
    }

     

    Your code with consistent variables and bringToFront() should work when all the page items are in a single Layer as is the case with your example file:

     

    var d = app.activeDocument;
    var sel = d.selection;
    for (i = 0; i < sel.length; i++) {
    sel[i].bringToFront();
    }

     

     

    2 replies

    rob day
    Community Expert
    rob dayCommunity ExpertCorrect answer
    Community Expert
    April 12, 2026

    Hi ​@dublove , Also, you are mixing j and i variables in you example, so the loop as you have written it doesn’t work. Here’s ​@m1b ’s code including the for statement and consistent variables:

     

    var d = app.activeDocument;
    var sel = d.selection;
    var topLayer = d.layers.firstItem();
    for (i = 0; i < sel .length; i++) {
    sel[i].itemLayer = topLayer;
    sel[i].bringToFront();
    }

     

    Your code with consistent variables and bringToFront() should work when all the page items are in a single Layer as is the case with your example file:

     

    var d = app.activeDocument;
    var sel = d.selection;
    for (i = 0; i < sel.length; i++) {
    sel[i].bringToFront();
    }

     

     

    dublove
    dubloveAuthor
    Legend
    April 13, 2026

    I wasn't paying attention.
    Thank you, I also learned: topLayer = d.layers.firstItem();

    m1b
    Community Expert
    Community Expert
    April 12, 2026

    Hi ​@dublove try this:

    // before your loop:
    var topLayer = doc.layers.firstItem();


    // then in your loop:
    sel[i].itemLayer = topLayer;
    sel[i].bringToFront();