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

2

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

Siddra answered 27/7, 2012 at 21:32 Comment(5)
Wouldn't string reference what you've just insterted?Frizz
@ŠimeVidas: I'm assuming the newly inserted one.Nahshun
@DavidThomas: Not if it's not a jQuery object.Nahshun
@Rocket, well, no; but that's why I was asking. Though I should, perhaps, have been more clear about the question? So: what is string, what is it that's expected/desired from this question?Frizz
Rocket is on the right track.Siddra
C
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

Castorena answered 27/7, 2012 at 21:35 Comment(1)
+1 - so far you're the only one who recognizes that the string becomes an element if you pass it into the jquery constructor/selectorTon
N
11

Try using insertAfter:

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

This will work if string is HTML.

Nahshun answered 27/7, 2012 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.