Reputation: 1670
I want to append a div
and afterwards to be able to select it. Can my code be optimized?
JS:
$someEle.append("<div><span>5</span><span>€</span></div>");
var $newELE = $someEle.find('div');
Upvotes: 1
Views: 23
Reputation: 318182
Create the elements first, that way they are easily accessible both before and after you append them
var div = $('<div />'),
span1 = $('<span />', {text : '5'}),
span2 = $('<span />', {text : '€'});
$someEle.append( div.append( span1, span2 ) );
div.css('color', 'red'); // use element
Upvotes: 2