views:

60

answers:

2

hi,

i got the following code

var zieldiv = $(this).attr('id');
$.ajax({
  url: 'index.php?params',
  type: 'POST',
  data: { color:thenewcolor, zieldiv:zieldiv },
  timeout: 50000,
  beforeSend: function() {                                            
    $("#" + zieldiv).css({background: "#" + thenewcolor} );
  }
});

I use this for dragging a small div into a bigger div. the variable at the beginnning would give out the string "test1". So in the ajax thing the css of a div which matches the var zieldiv should be updatet. Problem is, if i would place a string instead a var into the $(...) like $("#test1") it acts like its supposed to, the #test1 gets updated. but if i use the var $("#" + zieldiv) the parent div, which is surrounding the #test1-div gets updated. but i know for sure the var zieldiv contains the string "test1", because when passing it to php it contains "test1". so i think this is a little strange. do you have any ideas?

thanx, maschek

A: 

You could just do this:

var zieldiv = this.id;

$("#" + zieldiv).css({background: "#" + thenewcolor} );
$.ajax({
     url: 'index.php?params',
     type: 'POST',
     data: { 'color':thenewcolor, 'zieldiv':zieldiv },
     timeout: 50000
});

I'm guessing this is a simplified example, make sure you have the variable defined before your ajax call so it's passed into the closure, using something like this inside the callback won't work, because it doesn't know what you're talking about, this is in a different context when it returns.

Nick Craver
its not working like this. the thing is, the variable seems to have the right string in it, but its not recognized right, when placing it in the $(...). when is use a static value its working.
maschek
@maschek - If you `alert(zieldiv)` right after assigning it, what do you see?
Nick Craver
interesting! i placed an alert(zieldiv) and it actually gave two (!) alerts, first the one with the parent div, then the right one, the div i actually want to apply to. so its two divs inside the var, one to much.so, would there any way to split this var zieldiv in two parts?
maschek
@mascheck - That means your function is being called twice, can you update the question with the code around that's calling this? Most likely the selector that you're using to bind this event is including the parent as well.
Nick Craver
yes, it seems like that. but do you have any idea how to prevent that the event includes both?thanks, maschek
maschek
@maschek - What are the 2 IDs it's alerting, and can you add the html markup? There's an edit link underneath your question to add to it.
Nick Craver
you see, there is this var zieldiv. if i make an alert(zieldiv) i get the id i want, plus i get the parent id of that. BUT - if i pass that var zieldiv to a php file and save it in db, i get only the correct id, without the id of the parent object. i dont get it, how can that happen?
maschek
i thought about this a bit. i think the problem is, that the bigger div in which i drag the smaller div into, is inside another bigger div itself. maybe someone has an idea how to solve this? is there a possibilty to only select the last found id within $this?
maschek
A: 
    $(document).ready(function () {
$(\'#test1,#test2,#middle\').sortable();
$(\'#000,#ccc,#fff\').draggable({
helper: \'clone\',
});

$(\'#test1,#test2,#middle\').droppable({
accept: \'#000,#ccc,#fff\',   
hoverClass: \'txhover\',
drop: function(el, ui) {

if (ui.draggable.hasClass(\'txdropped\'))
return;
var drag = ui.draggable.clone();
drag.addClass(\'txdroppped\');
drag.id = \'cloneOf\' + ui.draggable.id;
$(this).append(drag);

var user = '.$user.'; 
var zieldiv = $(this).attr(\'id\');                                                                                                                                             
var thenewcolor = ui.draggable.attr(\'id\');
var backgroundcolor = this.style.backgroundColor;

$.ajax({
       url: \'index.php?eID=colors_ajax\',
       type: \'POST\',
       data: { color:thenewcolor, zieldiv:zieldiv, user2:user },
       timeout: 50000,
       beforeSend: function() {                                            
       $("#" + zieldiv).css({background: "#" + thenewcolor} );
       $("#tx-response").show();
       $("#tx-response").fadeOut(2000);
       }
});
}
});
});
maschek