views:

25

answers:

2

I am writing an Ajax application which uses a callback function to add a card to a players hand. the object is created correctly and the menu for each object is also created correctly.

when created the DOM object, in the callback function i use to add the object, i have some code like this:

$("#card"+cardNum).live('click',function(){                                                         
    $('#cardDiag'+cardNum).dialog('open');
}));

it works when the first card is created, but after i draw the second card, clicking on the first one causes it to now open up the menu for the second card. and after the second card is played (and i delete its menu) clicking on any card does nothing until a new card is drawn once again.

this basically is what i am doing in the callback handler for ajax.

function displayDrawnCardInHand(data){
var newCard = document.createElement('div');
//set some stuff on newCard
//and then add the cardyourHand = document.getElementById('hand');                                    yourHand.appendChild(newCard);
var cardMenu = document.createElement('div');                                      cardMenu.id= 'cardDiag' + data[cardNum];
//and then add the cardMenu to the DOM and call the click hander
A: 
  1. Provide more code
  2. Use Firebug (your HTML may be getting borked)

Also, I would recommend doing something like binding the event outside the $.ajax call:

$("div[id^=card]").live('click', function() { /* do binding */ });
tjko
i dont know how i am going to do it outside of the ajax call since there is several different cards i could draw as well as different menus for each of them
griff.steni.us
A: 

Edit

Now I see your problem: cardNum will always be evaluated for each click event, so it will be the same for each card no matter what the value was when you first assigned the click event. You'll have to store it in the #card as a data value:

$("#card"+cardNum).live('click',function(){
    num = $(this).data("num");                                               
    $('#cardDiag'+num).dialog('open');
})).data("num", cardNum);
Ian Wetherbee
i dont want to select all cards, i need to pull a different menu up for each different card. so, i am trying to add a new handler for the new dom object.
griff.steni.us
See updated, posting more code helps.
Ian Wetherbee
thanks, that looks like exactly what i needed. i was thinking it was a scope problem but this is my first week as a JS programmer and my firebug skills still need some work :D
griff.steni.us