I have a dynamically generated list, Here is my HTML code
<ol class="pending">
<li><a href="#" class="rendered">One</a></li>
<li><a href="#" class="rendered">Two</a></li>
<li><a href="#" class="rendered">Three</a></li>
<li><a href="#" class="rendered">Four</a></li>
<li><a href="#" class="rendered">Five</a></li>
<li><a href="#" class="rendered">Six</a></li>
</ol>
<ol class="patched"></ol>
When any particular link is clicked, It should moved to different list.
/*jslint browser: true*/ /*global $*/
$(document).ready(function(){
"use strict";
$('.rendered').on('click', function(){
$(this).toggleClass("rendered patched");
//$(this).parent().append($(this).wrap("<li></li>"));
$(this).appendTo("ol.patched");
});
});
So far the only difficulty is in having the anchor value in the <li> added to the new list as an <li>.
The result I keep getting is
<ol class="pending">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
<ol class="moved">
<a href="#" class="dld">One</a>
<a href="#" class="dld">Two</a>
<a href="#" class="dld">Three</a>
<a href="#" class="dld">Four</a>
<a href="#" class="dld">Five</a>
<a href="#" class="dld">Six</a>
</ol>
I'm not sure what I am misunderstanding about .append()
and .appendTo()
You should select parent of anchor and append it to ol
. JQuery .parent()
select parent of element.
$('.rendered').on('click', function(){
$(this).toggleClass("rendered patched");
$(this).parent().appendTo("ol.patched");
});
$('.rendered').on('click', function(){
$(this).toggleClass("rendered patched");
$(this).parent().appendTo("ol.patched");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol class="pending">
<li><a href="#" class="rendered">One</a></li>
<li><a href="#" class="rendered">Two</a></li>
<li><a href="#" class="rendered">Three</a></li>
<li><a href="#" class="rendered">Four</a></li>
<li><a href="#" class="rendered">Five</a></li>
<li><a href="#" class="rendered">Six</a></li>
</ol>
<ol class="patched"></ol>
Also you can write your code in one line
$('.rendered').on('click', function(){
$(this).toggleClass("rendered patched").parent().appendTo("ol.patched");
});