I was looking to do a .fadeIn()
animation chained to this .after()
, but this doesn't appear to be the way of doing it. Any suggestions?
$(clicked_item).parent().parent().parent().after(str).fadeIn("slow");
I was looking to do a .fadeIn()
animation chained to this .after()
, but this doesn't appear to be the way of doing it. Any suggestions?
$(clicked_item).parent().parent().parent().after(str).fadeIn("slow");
Share
Improve this question
edited Mar 10, 2017 at 14:26
Mohammad
21.5k16 gold badges56 silver badges84 bronze badges
asked Dec 22, 2011 at 0:12
ThinkingInBitsThinkingInBits
11.4k8 gold badges58 silver badges83 bronze badges
3 Answers
Reset to default 14You should use .insertAfter()
;
$(str)
.hide()
.insertAfter($(clicked_item).parent().parent().parent())
.fadeIn("slow");
In addition to @EmreErkan's answer, try minimizing your code. Use parents()
and select the id
or class
of the div you wish to add the text after instead of using parent()
three times:
$(str)
.hide()
.insertAfter($(clicked_item).parents(selector))
.fadeIn("slow");
EDIT: As pointed out in the comments, it is better to use closest()
instead of parents()
if you are targeting a single element, which using parents()
with a selector usually implies.
$.fn.after() will return the element it was run on (in this case $(clicked_item).parent().parent().parent()). If that is the element you want to fadeIn then I see no problem. If you want to fadeIn() the 'str' element instead I'd suggest doing this:
$(str).insertAfter($(clicked_item).parent().parent().parent()).fadeIn('slow');
A more stable way of getting a specific parent of an element that doesn't require you to change the number of .parent() calls if you change the HTML is to use .parents() together with a tag name:
$(clicked_item).parents('p').eq(0)
Change the 'p' to the element you want to reach.
Edit: woops, too late.