Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery append duplicates content

Tags:

jquery

append

I am new to jquery. I need help moving content. I am using the append method.

I want to be able to move each of the.links element into each .submitted element within the .node element. Right now append is adding all three .links to each .selected element. I have provided the code and a link to JSFiddle below.

Thanks for your help

Click here to view code in JSFiddle

jQuery

​$('.links').appendTo('.submitted');​​​​​​​​​​​​​​​​​

HTML

<div id="wrapper">
    <div class="node" class="clearfix">
        <h2>Node Title 1</h2>
        <div class="submitted">September 30</div>
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sollicitudin egestas mi, ac luctus orci eleifend pharetra</div>
        <div class="links">
            <a href="#">Node 1 Link 1</a> | 
            <a href="#">Node 1 Link 2</a>
        </div>
    </div>

    <div class="node" class="clearfix">
        <h2>Node Title 2</h2>
        <div class="submitted">September 29</div>
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sollicitudin egestas mi, ac luctus orci eleifend pharetra</div>
        <div class="links">
            <a href="#">Node 2 Link 1</a> | 
            <a href="#">Node 2 Link 2</a>
        </div>
    </div>

    <div class="node" class="clearfix">
        <h2>Node Title 3</h2>
        <div class="submitted">September 26</div>
        <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi sollicitudin egestas mi, ac luctus orci eleifend pharetra</div>
        <div class="links">
            <a href="#">Node 3 Link 1</a> | 
            <a href="#">Node 3 Link 2</a>
        </div>
    </div>
</div>​

CSS

#wrapper { width:600px; margin:0 auto; }
h2 { clear:both; }
.node { border-bottom:1px solid #CCCCCC; }
.submitted { float:left; width:200px; padding:10px; background-color:#CCCCCC; }
.content { float:left; width:360px; padding:10px; }
like image 696
Adrian Avatar asked Dec 06 '25 14:12

Adrian


2 Answers

when you do this

$('.links').appendTo('.submitted')

you are selecting all elements with class .links and adding them to all elements with class .submitted

EDIT

now i understood what you want :)

this fixes it

$(".links").each(function(){
    $(this).appendTo( $(this).prevAll('.submitted') );
});

see it working here: http://jsfiddle.net/RASG/SGgM8/8/

ss

like image 172
RASG Avatar answered Dec 08 '25 17:12

RASG


Try the following.

$('.links').each(function() {
   var $this = $(this);
   $this.appendTo($this.siblings('.submitted'));
})

http://jsfiddle.net/PwJNe/

Note that your markup is invalid, change this:

<div class="node" class="clearfix">

to:

<div class="node clearfix">
like image 36
undefined Avatar answered Dec 08 '25 19:12

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!