javascript - change order of elements after click() -
i wont change order of paragraphs(the 1 clicked user should @ top). came this:
<div> <p>11111111</p> <p>22222222</p> <p>33333333</p> </div> $("p").click(function(){ var = $(this).index(); if (a != 0) { $(this).clone().prependto("div"); $(this).remove(); } })
but works "once".
you're not far off, it's easier that: prepend element own parent prependto
:
$("p").click(function() { var $this = $(this); $this.prependto($this.parent()); });
<div> <p>11111111</p> <p>22222222</p> <p>33333333</p> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
if want, don't need jquery it:
$("p").click(function() { this.parentnode.insertbefore(this, this.parentnode.firstchild); });
<div> <p>11111111</p> <p>22222222</p> <p>33333333</p> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Comments
Post a Comment