Sjay
Sjay

Reputation: 801

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>');
    });
});

Upvotes: 2

Views: 1806

Answers (3)

calinaadi
calinaadi

Reputation: 1466

Change

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

to

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

Upvotes: 1

Artur Filipiak
Artur Filipiak

Reputation: 9157

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

Upvotes: 1

sinisake
sinisake

Reputation: 11338

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/

Upvotes: 1

Related Questions