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".

http://plnkr.co/edit/6vlwbilgi7fdbv6d0wt2?p=preview

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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -