In jQuery how can I correctly use a selector as var in a for loop?
<p>I'm trying to use a variable in my selector when applied to dynamically created div's (from an XML file) in a for loop. The previewDialog is only opening when I click on the very last div. How do I get the previewDialog to open when I click on EACH overview div?</p>

<pre><code>&lt;script type="text/javascript"&gt;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "Library.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;

var f = xmlDoc.getElementsByTagName("FeaturedEntry");
var x = xmlDoc.getElementsByTagName("BookEntry");
for (b = 0; b &lt; x.length; b++) {

  var bToString = b.toString();
  var overviewOpener = "#overview" + bToString;
  var previewDialog = "#preview" + bToString;

  // increase the default animation speed to exaggerate the effect
  $.fx.speeds._default = 1000;
  $(function () {
    $(previewDialog).dialog({
      autoOpen : false,
      show : "blind",
      hide : "explode"
    });

    $(overviewOpener).click(function () {
      $(previewDialog).dialog("open");
      return false;
    });

  });

  document.write(overviewOpener + " " + b.value + " " + bToString);
  document.write("&lt;div id='overview");
  document.write(b);
  document.write("'&gt;&lt;img id='cover' src='/ClientBin/");
  icon = x[b].getAttribute("Icon");
  document.write(icon);
  document.write("'/&gt;&lt;br&gt;&lt;strong&gt;&lt;a href='http://www.google.com'&gt;");
  title = x[b].getAttribute("Title");
  document.write(title);
  document.write("&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;");
  author = x[b].getAttribute("Author");
  document.write(author);
  document.write("&lt;br&gt;");
  document.write("Rating:");
  rating = x[b].getAttribute("Rating");
  numStars = Math.round(rating * 5);
  for (n = 0; n &lt; numStars; n++) {
    document.write("&lt;img src='/Media/star.png'/&gt;");
  }
  document.write("&lt;br&gt;&lt;a&gt;&lt;img id='button' src='Media/Small_iBookstore_Badge.gif' /&gt;&lt;/a&gt;");
  document.write("&lt;/div&gt;");

  document.write("&lt;div id='preview");
  document.write(b);
  document.write("'&gt;This is a test ");
  document.write(b);
  document.write("&lt;/div&gt;");

}
&lt;/script&gt;
</code></pre>
