$("#var_2_items").click(function() {
var x = "apple,oranges,grape fruit,pears,kiwi,mango,bananas";
var arr = x.split(',');
for(var i =0 ; i<=arr.length-1 ; i++){
$("#list1 ul").append('<li>'+arr[i]+'</li>');
$("#list1 ul").css("list-style-type","none")
}
});
$("#items_2_var").click(function() {
var x = "";
var listItems = $("#list1 ul li");
listItems.each(function(idx, li) {
x += $(li).text() + ",";
});
x = x.substring(0, x.length - 1); // remove the last char in your case the ,
});
<input type="button" value="items_2_var" id="items_2_var">
<input type="button" id="var_2_items" value = "var_2_items">
Just give ids as above. I tested this and it works