I created a simple todo kind of app using JavaScript. It is working fine but the code which I wrote is not proper. Please review my code and improve it if it is needed.
$(function () {
var todo = new Todo('contents');
$('.addBtn').on('click', function() {
var name = $(this).parent().find('input[type="text"]').val();
todo.add(name);
});
$('.contents').on('click', '.remove', function() {
var el = $(this).parent();
todo.remove(el);
});
$('.contents').on('click', '.update', function() {
var dom = $(this);
todo.addUpdateField(dom);
});
$('.contents').on('click', '.updateBtn', function() {
var el = $(this);
todo.update(el);
});
});
Here is my todo.js file
var Todo = function(c) {
var contents = $('.' + c),
name;
add = function (name) {
if(name != "") {
var div = $('<div class="names"></div>');
div.append('<span>' + name + '</span>');
div.append("<button class='update' class='update'>Edit</button>");
div.append("<button class='remove' name='remove'>Remove</button>");
contents.prepend(div);
$('.name').val('').focus();
}
return;
},
addUpdateField = function (dom) {
var name = dom.parent().find('span').text(),
field = $('<input type="text" value="' + name + '" />'),
update = $('<button class="updateBtn">Update</button>');
return dom.parent().html('').append(field).append(update);
},
update = function(el) {
var val = el.parent().find('input').val();
el.parent().html('<span>' + val + '</span>')
.append('<button class="update" class="update">Edit</button>')
.append('<button class="remove" class="remove">Remove</button>');
},
remove = function (el) {
return el.remove();
};
return {
add : add,
update : update,
remove : remove,
addUpdateField : addUpdateField
};
};
name;
(replace it with,
). \$\endgroup\$