2

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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/

1 Comment

Thank you so much for your valuable time and for solving my issue Thanks!
1

Change

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

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.