views:

469

answers:

3

Is there an easier/quicker way to get the element added using jQuery append:

How to get the $selectors element:

$container.append('<div class="selectors"></div>');
var $selectors = $('.selectors', $container);

I tried:

var $selectors = $container.append('<div class="selectors"></div>');

but that makes $selectors = $container

Maybe that's the quickest/best way. Just checking.

+5  A: 

Why not just:

var el = $('<div class="selectors"></div>');
$container.append(el);

?

Then you have access to 'el'.

cletus
Excellent! Thanks
slolife
+6  A: 

This is my favourite way of doing it:

var $selectors = $('<div class="selectors"></div>').appendTo(container);
Magnar
+1 - mine too. Do all you need to do with the newly created element before appending it to the DOM.
Russ Cam
This is good too! Thanks
slolife
+1  A: 
$selectors = $('<div/>').addClass('selectors').appendTo($container);
hobbs
Good info, thanks. It is a little verbose for me compared to the other answers, but I wonder if it does better performance wise? Performance is not an issue in my particular situation, but maybe for others.
slolife