1

I have an array (via ajax) that looks like this:

data[i].id: gives the id of user i
data[i].name: gives the name of user i

I want to output the array like this:

X Leonardo Da Vinci
X Albert Einstein
X William Shakespeare
...

The X is an image (x.gif) that must be clickable. On click, it must go to functiontwo(), passing the parameter data[i].id. Functiontwo will open a jquery dialog with the question "Delete id data[i].id"?

I know this can't be too hard to do, but I can't seem to figure it out... This is what I have so far:

function functionone() {


    $.ajax({
        type : 'POST',
        url : 'post.php',
        dataType : 'json',
        success : function(data){

            var message = "";
            var i = 0;


            while (i < (data.length - 1))
                            {
                 var myvar = data[i].id;
                 message = message + "<div class=" + data[i].id + "><img src=x.gif></div>" + data[i].name + "<br />";


                 $('#somediv').html(message).fadeIn('fast');
                  $("." + data[i].id + "").click(function () {
                  functiontwo(myvar);
                });
                i++;

            }



        }


    });

}

 function functiontwo(id) {
 ...}

I know why this isn't working. Var i gets populated again and again in the while loop. When the while loop stops, i is just a number (in this case the array length), and the jquery becomes (for example):

$("." + data[4].id + "").click(function () {
                  functiontwo(myvar);
                });

, making only the last X clickable.

How can I fix this? Thanks a lot!!!

EDIT:

This is my 2nd function:

 function functiontwo(id) {

  $("#dialogdelete").dialog("open");

     $('#submitbutton').click(function () {

        $('#submitbutton').hide();
        $('.loading').show();
                    $.ajax({
        type : 'POST',
        url : 'delete.php',
        dataType : 'json',
        data: {
            id : id
        },
        success : function(data){

            var mess = data;
            $('.loading').hide();
            $('#message').html(mess).fadeIn('fast');


        }
    });

    //cancel the submit button default behaviours
    return false;



        });


    }

In delete.php there's nothing special, I used $_POST['id'].

3
  • Step one is use bind, not click, to create the event-listener. Commented Oct 25, 2011 at 18:12
  • Could you be more specific? I tried $("." + data[id].id + "").bind('click',function() { make_event_handler(myvar)}); but that didn't work either... Commented Oct 25, 2011 at 18:23
  • See my answer belove. the class-solution is easier Commented Oct 25, 2011 at 18:25

3 Answers 3

1

As I pointed out in my comment. The problem is the .click part. Either use bind, or use a class for all the elements, and a click-event like this $('.classnamehere').live('click',function () { // stuff });

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed the .click part to $("." + data[i].id + "").live('click',function () {functiontwo(myvar)});. This does make every X clickable, however, the parameter that gets passed to the function (in the case myvar) is always the same: the id of the last user.
You need to pass data[i].id as id, not class. And apply the same class for every object. And the live-listner can be created outside the function. See my new answer
1
function functionone() {

    $.ajax({
        type : 'POST',
        url : 'post.php',
        dataType : 'json',
        success : function(data){

            var message = "";
            var i = 0;

            while (i < (data.length - 1))
                            {
                 var myvar = data[i].id;
                 message = message + "<div class=\"clickable\" id=" + data[i].id + "><img src=x.gif></div>" + data[i].name + "<br />";

                 $('#somediv').html(message).fadeIn('fast');
                i++;
            }
        }
    });
}

$('.clickable').live('click',function () {
    alert($(this).attr('id') + ' this is your ID');
});

4 Comments

After a closer look, it appears it's not working as it should be. It's passing the parameter, but it "remembers" it too long:clicking X goes to functiontwo(id) which gives the user a jquerypopup with the message "delete this user?", and a submit button. When you click on the X next to user1,close the dialog(without pushing the submit button),click the X next to user3 and click on submit, not only user3 get's deleted (which is what it needs to do), but user1 gets deleted as well... this is off course not what I'm after... How can I fix this final issue? thx!
Well, that depends on how your 2nd function is working. The problem is not what is posted here. Have you checked your code that the IDs are unique?
Yes, I'm sure the IDs are unique. I'll edit my first post and put my 2nd function there too.
I think your problem is that when you click the box, you don't reset the content of the dialog-box. Use Firebug (and FF), to check when the script (delete.php) is loaded.
0

The usual trick is create a separate function to create the event handler. The separate function will receive i as a parameter and the generated event will be able to keep this variable for itself

make_event_handler(name){
    return function(){
        functiontwo(name);
    };
}

...

$("." + data[i].id + "").click( make_event_handler(myvar) );

2 Comments

Thought this would have done the trick, but apparently, nothing's changed. The last X is clickable, the rest isn't...
That should have done the trick. Can you provide a minimal functioning example (perhaps at JSFiddle)

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.