Search code examples
javascriptjquerydomjquery-after

after() inserting element, then getting it back


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


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