Monday, February 1, 2016

after() / before()

Sometimes you want to insert something into the DOM, but you don't have any good hooks to do it with; append() or prepend() aren't going to cut it and you don't want to add an extra element or id. These two functions might be what you need. They allow you to insert elements into the DOM just before or after another element, so the new element is a sibling of the older one. 1 2 $('#child').after($('
')).text('This becomes a sibling of #child')); $('#child').before($('
')).text('Same here, but this is go about #child')); You can also do this if you're working primarily with the element you want to insert; just use the insertAfter() or insertBefore functions. 1 $('
I\'ll be a sibling of #child
').insertAfter($('#child'));

0 comments:

Post a Comment