8

I don't think I'm correctly understanding jQuery chaining. I'm looping through an array and trying to add div elements to my wrapper CSS class with each div element having a class of 'click' and custom css top and left attributes, like this:

$('<div></div>').appendTo('.wrapper').addClass('click').css('top',click.y).css('left'.click.x);

But it fails to work as expected - it adds one div element, sets my .wrapper div's class to 'click' and then stops.

If I remove

.css('top',click.y).css('left'.click.x);

it works as expected - adding new div elements to the wrapper div.

How can i get this to work properly? Thanks

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
CJD
  • 782
  • 3
  • 10
  • 20

2 Answers2

24

Use a JSON object for .css.

.css({
   'left' : click.x,
   'top' : click.y
 });
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    That's just a regular JS object. A JSON object would be a string representation of the JS object syntax. – treeface Feb 23 '11 at 17:59
  • 2
    Mmm acutally the main problem I had (there were many) was because i wasn't adding " + 'px'" to my values. – CJD Feb 23 '11 at 21:21
0

try this

$('<div>/div>').appendTo('.wrapper').addClass('click').css({
    'top': click.y,
    'left': click.x
});
Hussein
  • 42,480
  • 25
  • 113
  • 143