i need help to add element on html using jquery, i'm new on this territory, i've tried several method and failed
here's the HTML
<div id="relatedpostspro_0_1" class="rpp rpp_isotope" style="width:auto;">
<div class="rpp_container">
<legend>Related Posts</legend>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
</div>
and i want to add this small piece
<div class="stripe-line"></div>
so it will result to:
<div id="relatedpostspro_0_1" class="rpp rpp_isotope" style="width:auto;">
<div class="rpp_container">
<legend>Related Posts</legend>
<div class="stripe-line"></div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
</div>
using these 2 jquery from first 2 answer
$('.rpp_container').append('<div class="stripe-line"></div>');
and
var line = $('<div />').addClass('stripe-line');
line.appendTo('.rpp_container');
resulting the line added at the bottom, not right after <legend>Related Posts</legend>
Thanks a lot!
-
Hey, post your jQuery code as well so we know why your methods didn’t work.Sebastian Simon– Sebastian Simon2015年04月26日 15:30:34 +00:00Commented Apr 26, 2015 at 15:30
-
i want to add stripe lines, tried several times with css using pseudo element :after and unsuccessful because it has repeat property. can't put width: 100%; because it's not start at left: 0; using overflow: hidden; not helping. the only things work only adding new element. tried with search replace not working, i'm new on this thing.somuch72– somuch722015年04月26日 16:04:57 +00:00Commented Apr 26, 2015 at 16:04
4 Answers 4
I believe you're in need of after()
("Insert content, specified by the parameter, after each element in the set of matched elements."):
$('.rpp_container legend').after('<div class="stripe-line"></div>');
Which produces:
<div id="relatedpostspro_0_1" class="rpp rpp_isotope" style="width:auto;">
<div class="rpp_container">
<legend>Related Posts</legend>
<div class="stripe-line"></div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
</div>
There are many different methods to find / modify DOM elememts, append
has already been shown:
$(document).ready(function() {
$('.rpp_container').append('<div class="stripe-line"></div>');
});
You can also use after
with find
:
$(document).ready(function() {
$('.rpp_container').find('legend').after('<div class="stripe-line"></div>')
});
A few other helpful methods for traversing the DOM:
-
hi.. it works just by using
after()
. sorry for my igonrance, why usingfind()
? i often saw jquery code using that, my guess is that to avoid wrong element?somuch72– somuch722015年04月26日 16:32:01 +00:00Commented Apr 26, 2015 at 16:32 -
Over time, your HTML may become more complicated, using
find
will help make it easier to select the correct element, this method basically allows us to search through the descendants of a set of elements in the DOM tree.Michael Doye– Michael Doye2015年04月26日 16:37:02 +00:00Commented Apr 26, 2015 at 16:37
Use after
$('.rpp_container legend').after('<div class="stripe-line"></div>');
Reference: https://api.jquery.com/after/
-
work like a charm! thanks a lot. this community cool. usually it takes me 5 hours just to get 5-10 lines code, next time i will ask here after 3 hours trying :Dsomuch72– somuch722015年04月26日 16:25:59 +00:00Commented Apr 26, 2015 at 16:25
Try this
$('.rpp_container').append('<div class="stripe-line"></div>');