tags:

views:

105

answers:

2

First of all, do not be overwhelmed by the long code, I just put it for reference...I have a function that runs preg_replace on each comment users post and puts matches in a jquery dialog box with a matching open-link. For example, if there is a paragraph with two matches, they will be put inside two divs, and a jquery dialog function will be echoed twice; one for each div.

While this works for one match, if there are multiple matches, it does not. I am not sure how to distribute unique ids at a point in time for each of the divs and matching dialog open-scripts. Keep in mind, I removed the preg replace function since it kind of complicates the problem. If anyone has any ideas, they will be greatly appreciated.

<?php
    $id=uniqid();
    $id2=uniqid();

    echo "<div id=\"$id2\">
      </div>";
?>   
     $.ui.dialog.defaults.bgiframe = true;
     $(function() {
      $("<?php echo"#$id2"; ?>").dialog({hide: 'clip', modal: true
    ,width: 600,height: 350,position: 'center',
    show: 'clip',stack: true,title: 'title', minHeight: 25,
    minWidth: 100, autoOpen: false});

       $('<?php echo"#$id"; ?>').click(function() {
       $('<?php echo"#$id2"; ?>').dialog('open');
      })
      .hover(
       function(){ 
        $(this).addClass("ui-state-hover"); 
       },
       function(){ 
        $(this).removeClass("ui-state-hover"); 
       }
      ).mousedown(function(){
       $(this).addClass("ui-state-active"); 
      })
      .mouseup(function(){
        $(this).removeClass("ui-state-active");
      });
     });
+2  A: 

Have you tried something like and only displaying the function once.

<?php    
    echo "<div class="dialog"></div>";
?>   
$(document).onload(function() {
     $.ui.dialog.defaults.bgiframe = true;
     $(function() {
      $(".dialog").dialog({ /* ... */

EDIT (again)

Some Text
<a href="" class="open">Open Dialog 1<span class="box">Dialog Content 1</span></a> - 
<a href="" class="open">Open Dialog 2<span class="box">Dialog Content 2</span></a> - 
<a href="" class="open">Open Dialog 3<span class="box">Dialog Content 3</span></a>
ABC 123

$(".dialog").dialog({
    hide: "clip",
    modal: false,
    width: 600,
    height: 350,
    position: "center",
    show: "clip",
    stack: true,
    title: "title",
    minHeight: 25,
    minWidth: 100,
    autoOpen: false,
    bgiframe: true
});
$("a.open").click(function() {
    $(".box").eq($(this).index()).dialog("open");
    return false;
});

Tested and works. The problem was that when JQuery replaces the dialog it does not so in the same position. Checking which index the click is from will match up to the dialog box you are trying to opening.

bigstylee
I don't understand your suggestion. How can it work if the function is only shown once and there are a bunch of the same ids?
Scarface
With this solution there is no need to generate unique ids for your div elements because your attaching your dialog event to all matching elements class="dialog" => $(".dialog")
bigstylee
There does not necessarily have to be a one to one relationship between DIVs and Jquey functions. $("a").click(function(){alert("Hello World!");}); would attach this event to all links on your web page.
bigstylee
I am having a bit of trouble reproducing that. I tried as a test 3 links with the same id=open and three dialog boxes with id=dialog and your function with id= dialog displayed once. The function would not work properly unless I made everything unique and showed the function three times. Maybe I did something wrong or you forgot to mention something. I displayed what I did as an answer.
Scarface
k I tried to post it but I am having difficulty. I know your $("a").click(function(){alert("Hello World!");}); would work on all links but I am not sure if dialog boxes are the same. Can you maybe post an example that worked for you?
Scarface
Please see update
bigstylee
After some playing around I have provided a working solution. Hope this helps!!! (see update (again))
bigstylee
I tried your solution lol with three class=dialog divs, and right away it did not make sense. How can I have three openers and divs with the same id and still expect that a specific div will open for a specific link. I do not want to open them at the same time. Anyway it did not work because the javascript would not function. PS what version of Jquery are you using.
Scarface
Can you post a full solution (including divs) that will match unique links to unique divs so that each link has a div. I hope what you are saying is true because it would save me a lot of code, but I am a little skeptical. Thanks for sticking it out with me though.
Scarface
I am using JQuery 1.4.2 and UI 1.8. Just been looking around for an alternative solution and found this one which is probably the best solution I have seen so far (http://stackoverflow.com/questions/725841/jquery-dialogue-multiple-on-the-same-page)
bigstylee
Oh and Dont forget to put your JQuery code inside $(document).ready(function() { }
bigstylee
thanks bigstylee I ended up using unique ids for every one by using preg_replace_callback http://stackoverflow.com/questions/2619437/is-it-possible-to-preg-replace-unique-variables-into-a-stringIt was really the only option for what I was trying to do
Scarface