Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after() inserting element, then getting it back [duplicate]

Possible Duplicate:
Why does jQuery .after() not chain the new element?

This reference code:

$("#id").after(string);

Does a pretty good job inserting the element of the string where its need to.

How can I get a reference to newly inserted HTML element (string)?

like image 691
jacktrades Avatar asked Jul 27 '12 21:07

jacktrades


2 Answers

var string = '<div id="some_HTML"><span>hello kitty</span></div>';

$jq_elem = $(string);  //if it's not a jQuery object, make it one

$("#id").after($jq_elem); //insert into DOM

$jq_elem.css('color', 'red'); //still available
​

FIDDLE

like image 65
adeneo Avatar answered Oct 12 '22 23:10

adeneo


Try using insertAfter:

var $str = $(string).insertAfter('#id');

This will work if string is HTML.

like image 31
Rocket Hazmat Avatar answered Oct 12 '22 23:10

Rocket Hazmat