Append List values using JQuery

using JQuery append how to push list values to another div section on button click

For example:

I have two list values

1.Adam  

2.Lance

if i click on add button the Adam value should be added to another div section same for lance.. etc

https://jsfiddle.net/oa2hfLum/3/

help me out in achieving it Thanks!

html

<div>
<ol id="sortable">

</ol>
</div>

<div>
        <ul id="draggable">
            <li>
                <div>
                    <label class='lbl'>
                        Lance
                    </label>
                    <button>Add To Top container</button>
                </div>
            </li>
            <li>
                <div>
                    <label class='lbl'>
                        Adam
                    </label>
                    <button>Add To Top container</button>
                </div>
            </li>

        </ul>
    </div>

JQuery

$(document).ready(function() {
    $("button").click(function () {
        $("#sortable").append('<li></li>');
    });
});

Answers:

Answer

You can get the label text using .prev() and .text()

$("button").click(function () {
    var label = $(this).prev().text();
    $("#sortable").append('<li>'+label+'</li>');
});

JSFiddle demo

Answer

I think that you could use this:

$(document).ready(function() {
    $("button").click(function () {
        $("#sortable").append('<li>'+$(this).prev('label').text()+'</li>');
    });
});

Demo: https://jsfiddle.net/oa2hfLum/4/

Answer

Change

$("#sortable").append('<li></li>');

to

$("#sortable").append('<li>'+$(this).parent().find('label').html()+'</li>');

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.