Mark
Mark

Reputation: 4873

Multiple Jquery Dialogs on one page

I'm using jquery dialog popup. I have multiple div's (dynamically created) that require the pop-up on a single page. My current issue is when I click the .open, all (10) pop-ups are opening, how can I trigger only one?

My html is as follows:

      <a class="open" href="#">
          <img src="/images/someImage1.jpg" />
      </a>

    <div class="dialog" title="Gives Dialog Title"><!-- This is display:none in css-->
    <p> Dialog text and stuff</p>
   </div>

   <a class="open" href="#">
          <img src="/images/someImage1.jpg" />
      </a>

    <div class="dialog" title="Gives Dialog Title"><!-- This is display:none in css-->
    <p> Dialog text and stuff</p>
   </div>

My jquery is as follows:

   <script type="text/javascript"> // added .dialog:disaplay:none; to desktop.css


  $(document).ready(function () {

      $('a.open').prop('id', function (i) {
          return '' + (i + 1);
      });

      $(".dialog").dialog({
              autoOpen: false,
              draggable: false,
              width: "300px",
              modal: true,
              buttons: {
                  "Close": function () {
                      $(this).dialog('destroy').remove()
                  }
              }

          });

      $("#1,#2,#3,#4,#5,#6,#7,#8,#9,#10")

   .click(function () {
  alert($(this).attr("id"));

  $(".dialog").dialog("open");
  return false;
   });
  });

    </script>

Upvotes: 0

Views: 6255

Answers (3)

DroidOS
DroidOS

Reputation: 8890

If I am reading your code correctly what you are doing is

$('.dialog').dialog('open');

i.e. you are getting hold of all elements that use the dialog class. Naturally, that opens all your dialogs all at once. If you rewrite your markup along the lines

<div class="dialog" title="Gives Dialog Title" id='diaOne'><!-- This is display:none in css-->
  <p> Dialog text and stuff</p>
</div>

and then do

$('#diaOne').dialog('open');//grab just the dialog bearing the id diaOne.

that should I suspect do the trick

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

I feel the need to tell you to just use a class as your click handler

$(".open").click(function(e) {
    e.preventDefault();
    $(this).next(".dialog").dialog("open");
});

There's no need for the ID's if you're not using them.

Upvotes: 0

Anthony Atkinson
Anthony Atkinson

Reputation: 3248

This should work (or a variant of it).

$("#1,#2,#3,#4,#5,#6,#7,#8,#9,#10").click(function () {
  alert($(this).attr("id"));

  $(this).next(".dialog").dialog("open");
  return false;
});

Upvotes: 2

Related Questions