Question

I was wondering how I could put a button on each item in my JQuery sortable. Right now I have a button on the top of the screen that adds the user input in the autocomplete to the sortable. Here is my .js code that adds new items to my sortable:

$(".addButton").click(function(e) {
    e.preventDefault();
    // set var item to be the string inputted by the user
    var item = $("input[name='inputItem']").val(); //where 'inputItem' is the name of the <form> which contains the <input>
    // parses input string, splitting at commas into liArray containing substrings as elements
    var liArray = item.split(", ");
    // for loop to add each brew to the sortable list (length-1 because last element in array is empty string)
    for (var i = 0; i < liArray.length-1; i++) {
        // sets var $li to the string in the ith index of liArray
        var $li = $("<li class='ui-state-default'/>").text(liArray[i]);

        // adds var $li to gui
        $("#sortable").append($li);
    };

    $("#sortable").sort();

    // refreshes the page so var $li shows up
    $("#sortable").sortable("refresh");
});

My idea was to add something like

<button>Button!</button> 

to the this line:

var $li = $("<li class='ui-state-default'/>").text(liArray[i]);

But I dont know how to implement it exactly. Any help would be appreciated. Thanks!

Was it helpful?

Solution

Try this.

$("<li class='ui-state-default'/>").text(liArray[i]).append('<button>Button!</button>');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top