Sunday, 27 July 2014

Dynamically create a ul li with appended text

How to dynamically create a ul li with appended text

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="jquery-1.8.2.min.js"></script>
<script>
var MyArrayData = [];
$(document).ready(function () {
disp($(".MyData").toArray());
jQuery.each(MyArrayData, function (i, val) {
if (val.length) {
$('<li />', { html: i }).appendTo('ul.justList')
}
});
});
function disp(divs) {
for (var i = 0; i < divs.length; i++) {
MyArrayData.push(divs[i].innerHTML);
}
}
</script>
<style>
.MyData
{
display: none;
}
</style>
</head>
<body>
<ul class="justList"></ul>
<div class="MyData">
One
</div>
<div class="MyData">
Two
</div>
<div class="MyData">
Two
</div>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub