title: each in jquery after append
body: <p>I see now I was copy pasting the wrong text/question, that didn't make sense at all! Sorry for that! I found the error in my code. Here is a working version:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;style type="text/css"&gt;
  body{
  margin:0;
  }
  #gallery{
  width:100%;
  height:100%;
  position:fixed;
  left:0;
  top:0;
  background:#000;
  opacity:0.9;
  }
  .child{
  width:auto;
  height:auto;
  position:fixed;
  left:0;
  top:0;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="click_me"&gt;click me&lt;/div&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function () {
  $('#click_me').click(function(){
  $('body').append('&lt;div id="gallery"&gt;&lt;/div&gt;&lt;div class="child" style="width:300px;height:600px;background:#0F0"&gt;&lt;/div&gt;&lt;div class="child" style="width:500px;height:400px;background:#C60"&gt;&lt;/div&gt;&lt;div class="child" style="width:600px;height:200px;background:#390"&gt;&lt;/div&gt;');
     height_of_window = $('#gallery').height();
     width_of_window = $('#gallery').width();
     max_height = height_of_window - 100;
     max_width = width_of_window - 100;
     $('.child').each(function () {
        var $this = $(this);
        height_of_child = $this.height();
        width_of_child = $this.width();
        if (width_of_child &gt;= max_width) {
           $this.css({
              'max-width': max_width + 'px'
           })
        }
        if (height_of_child &gt;= max_height) {
           $this.css({
              'max-height': max_height + 'px'
           })
        }
        margin_top = (height_of_window - $this.height()) / 2;
        margin_left = (width_of_window - $this.width()) / 2;
        $this.css({
           'margin-top': margin_top + 'px',
           'margin-left': margin_left + 'px'
        })
     }); // end click
     }); // end each
  }); // end document redy
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>