How to append() an element and set its style with css() at the same time with jQuery

Posted by Acorn on Stack Overflow See other posts from Stack Overflow or by Acorn
Published on 2010-04-25T12:18:06Z Indexed on 2010/04/25 12:23 UTC
Read the original article Hit count: 219

Filed under:
|
|
|

I tried: $('#canvas').append('<div class="tile"></div>').css({left: leftPos, top: topPos});, but that sets the style of #canvas rather than the appended element.

I then tried: $('#canvas').append(('<div class="tile"></div>').css({left: leftPos, top: topPos}));, but that gives the error "Object <div class="tile"></div> has no method 'css'".

How can I add the element and set its style at the same time?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript