after() inserting element, then getting it back

7.1k views Asked by At

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)?

2

There are 2 answers

1
adeneo On BEST ANSWER
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

0
gen_Eric On

Try using insertAfter:

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

This will work if string is HTML.