15

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

Community
  • 1
  • 1
jacktrades
  • 7,224
  • 13
  • 56
  • 83

2 Answers2

30
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

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • 2
    +1 - so far you're the only one who recognizes that the string becomes an element if you pass it into the jquery constructor/selector – Kristian Jul 27 '12 at 21:37
11

Try using insertAfter:

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

This will work if string is HTML.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337