Reputation: 289
Trying to not allow any blank entries added to my list. I wrote the code and it seems to work fine. But I want to know is there a better way to write this. It just looks so weird to me that I feel like even though it works it just doesn't feel right. Any suggestions would be greatly appreciated.
Here's the code:
$("#item-add").keypress(function (enter){
var press = enter.which;
var addItem = $('#item-add').val();
if(press == 13) {
if (addItem == "") {
alert("You must enter an item to add to list.")
$(".check-list").append('<li><input type="checkbox" class="blank-box">' + addItem + '</li>');
$('.blank-box').remove();
} else {
$(".check-list").append('<li><input type="checkbox" class="check-box">' + addItem + '</li>');
};
$("#item-add").val('');
}
});
Upvotes: 1
Views: 25
Reputation: 25527
There is no need to add and remove .blank-box
element, just use,
$("#item-add").keypress(function (enter) {
var press = enter.which;
var addItem = $('#item-add').val();
if (press == 13) {
if (addItem != "") {
$(".check-list").append('<li><input type="checkbox" class="check-box">' + addItem + '</li>');
} else {
alert("You must enter an item to add to list.")
}
$("#item-add").val('');
}
});
Upvotes: 3