Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting jQuery-inserted DOM elements without requerying for element after insertion

Assuming I insert anything in my DOM with jQuery, like so:

$('#someselector').prepend('<img src="/img/myimage.gif" id="someid" />');

Is there a way in jQuery to get a jQuery object referencing this new image, without having to do an extra search like

var myImage = $('#someselector #someid');

???

like image 736
Sorcy Avatar asked May 10 '11 12:05

Sorcy


1 Answers

Make it into an jQuery object before prepending it:

var $img = $('<img src="/img/myimage.gif" id="someid" />');    
$('#someselector').prepend($img);    
$img.foo();
like image 185
Tatu Ulmanen Avatar answered Sep 18 '22 01:09

Tatu Ulmanen