1

I use the JQuery UI Library and the Dialog function.

I would like to create more than one triggers inside my javascript.

This is my original JS:

$j(function() {
    $j( "#dialog1" ).dialog({
        autoOpen: false,
        show: "slide",
        hide: "explode"
    });
    $j( "#dialog2" ).dialog({
        autoOpen: false,
        show: "slide",
        hide: "explode"
    });
    ...

    $j( ".opener1" ).click(function() {
        $j( "#dialog1" ).dialog( "open" );
        return false;
    })
    $j( ".opener2" ).click(function() {
        $j( "#dialog2" ).dialog( "open" );
        return false;
    })
    ...
});

I need at least a dozen of those triggers. So, I though, let's make a php While loop.
Something like this:

<?php
    $i = 1;
    while ($i <= 10) {
        echo '$j( "#dialog'.$i.'" ).dialog({
        autoOpen: false,
        show: "slide",
        hide: "explode"
        });';
    $i++;
    }

    $q = 1;
    while ($q <= 10) {
        echo '$j( ".opener'.$q.'" ).click(function() {
        $j( "#dialog1" ).dialog( "open" );
            return false;
        })';
    $q++;
    }?>

And for the first while, it's all working just fine. It does the trick. But for the second while, the page just ignores the whole JS/PHP block...

What am I doing wrong?

3
  • im sure you could writer smarter js and not need the php Commented Sep 7, 2011 at 22:14
  • So, you suggest to write it all in javascript? Commented Sep 7, 2011 at 22:16
  • im sure it would be possible, i would use a class for the trigger then use the septate id's for the element actions. Commented Sep 7, 2011 at 22:18

5 Answers 5

4

This line in your second while loop:

$j( "#dialog1" ).dialog( "open" );

Should that not be:

$j( "#dialog' . $q . '" ).dialog( "open" );

At the moment all of your click handlers will open #dialog1 which I guess is not what you're trying to do

You could do the whole thing in js though as suggested above:

for (var i=1;i<=10;i++) {
  $j('#dialog' + i).dialog({
    autoOpen: false,
    show: "slide",
    hide: "explode"
  });

  $j('.opener' + i).click(function() {
    $j( "#dialog" + i).dialog( "open" );
    return false;
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Is it better to do it all in js?
Unless you need to add dynamic variables that are only available in PHP, yes it's better to put javascript in a .js file or between <script> tags
In this case, it indeed needs dynamic variables! But thanks, you solved my problem!
Sorry by dynamic I meant variables that are only available on the server side, data from a database for example
that's exactly what I meant. Data from my database is needed in the page.
2

You could generate all of the above using only jQuery by linking all the dialogs using one class (which will become their selector) and link the openers with their respective dialogs as found in the example below:

http://jsfiddle.net/Awh7D/

Note: Better ways to link the openers with their respective dialogs can be developed.

Comments

2

At least in the first case you should be able to use "multiple selectors", like in

$j( "#dialog1, #dialog2, #dialog3" ).dialog({
    autoOpen: false,
    show: "slide",
    hide: "explode"
});

EDIT (after a "working break")

the second part, where you reference the object inside the function, should work like this:

$j( ".opener1, .opener2, .opener3" ).click(function() {
    $j(this).dialog( "open" );
    return false;
});

Sorry I can't simulate that right now, so it's a bit walking on thin ice - you will forgive me.

3 Comments

"At least in the first case"... Why not in the second? Good hint though!
This method requires a certain amount of maintenance in the future and it's not generic to the point where he can add as many dialogs and openers as he wishes without worrying about modifying his selectors.
@Link ... agree - +1 and upvote for my personal favourite amongst the jsfiddle examples ;)
0

I agree with Clive :

$q = 1;
    while ($q <= 10) {
        echo '$j( ".opener'.$q.'" ).click(function() {
        $j( "#dialog1" ).dialog( "open" );
            return false;
        })';
    $q++;
    }?>

Should be :

$q = 1;
    while ($q <= 10) {
        echo '$j( ".opener'.$q.'" ).click(function() {
        $j( "#dialog'.$q.'" ).dialog( "open" );
            return false;
        })';
    $q++;
    }?>

Comments

0

You could also share a class amongst the dialogs/openers. Here's a fiddle example

http://jsfiddle.net/ZbMcA/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.