Question

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

Was it helpful?

Solution

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

OTHER TIPS

Try using insertAfter:

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

This will work if string is HTML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top