javascript - why doesn't jquery insert created span into two divs? -
i have following html:
<div id="one"></div> <div id="two"></div>
when run following code in jquery:
$(function () { var span = $("<span>name</span>"); $("#one").append(span); $("#two").append(span); });
the span added last div:
<div id="one"></div> <div id="two"><span>name</span></div>
i'd expect added both divs, why it's not?
each dom
element can connected 1 specific parent. can't append same dom
element 2 dom parents. refer so
in case, must clone
node.
$(function () { var span = $("<span>name</span>"); $("#one").append(span); $("#two").append(span.clone()); });
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div id="one"></div> <div id="two"></div>