Skip to main content
added 1329 characters in body
Source Link
J.Zil
  • 387
  • 2
  • 16

I've worked hard on this code and it functions exactly as I wanted, but I've been told it is a little bit unreadable. My focus is to get the code as clean as possible, and have things as efficient as possible.

This is the code I am working with: http://jsfiddle.net/xT5X5/6/

//latest
var maxFields = 10;

$('.form').on('click', '.add', function () {
      var value_src = $(this).prev();
       var container = $(this).parent().prev();
    if ($.trim(value_src.val()) !== '') {
        if (container.children().length < maxFields) {
            var value = value_src.val();
            var html = '<div class="line">' +
                '<input class="accepted" type="text" value="' + value + '" />' +
                '<input type="button" value="X" class="remove" />' +
                '</div>';

            $(html).appendTo(container);
            value_src.val('');
        } else {
            alert("You tried to add a field when there are already " + maxFields);
        }
    } else {
        alert("You didn't enter anything");
    }
  
})
    .on('click', '.remove', function () {
       $(this).parents('.line').remove();
});

$(".current").keyup(function(e) {
    if (e.which == 13) {
        $(this).next("input").trigger("click");
    }
});

$(document).on("keyup",".accepted",function(e) {
    if (e.which == 13) {
        $(this).closest('.copies').next().find(".current").focus();
    }
});

Can anyone give me any pointers on how I've done, and if there are any improvements that you can spot. Any feedback would be much appreciated!

Thank you.

I've worked hard on this code and it functions exactly as I wanted, but I've been told it is a little bit unreadable. My focus is to get the code as clean as possible, and have things as efficient as possible.

This is the code I am working with: http://jsfiddle.net/xT5X5/6/

Can anyone give me any pointers on how I've done, and if there are any improvements that you can spot. Any feedback would be much appreciated!

Thank you.

I've worked hard on this code and it functions exactly as I wanted, but I've been told it is a little bit unreadable. My focus is to get the code as clean as possible, and have things as efficient as possible.

This is the code I am working with: http://jsfiddle.net/xT5X5/6/

//latest
var maxFields = 10;

$('.form').on('click', '.add', function () {
      var value_src = $(this).prev();
       var container = $(this).parent().prev();
    if ($.trim(value_src.val()) !== '') {
        if (container.children().length < maxFields) {
            var value = value_src.val();
            var html = '<div class="line">' +
                '<input class="accepted" type="text" value="' + value + '" />' +
                '<input type="button" value="X" class="remove" />' +
                '</div>';

            $(html).appendTo(container);
            value_src.val('');
        } else {
            alert("You tried to add a field when there are already " + maxFields);
        }
    } else {
        alert("You didn't enter anything");
    }
  
})
    .on('click', '.remove', function () {
       $(this).parents('.line').remove();
});

$(".current").keyup(function(e) {
    if (e.which == 13) {
        $(this).next("input").trigger("click");
    }
});

$(document).on("keyup",".accepted",function(e) {
    if (e.which == 13) {
        $(this).closest('.copies').next().find(".current").focus();
    }
});

Can anyone give me any pointers on how I've done, and if there are any improvements that you can spot. Any feedback would be much appreciated!

Thank you.

edited title
Link
J.Zil
  • 387
  • 2
  • 16

Jquery field add code - Improvements in reliabilityreadability, compactness or efficiency

Source Link
J.Zil
  • 387
  • 2
  • 16

Jquery field add code - Improvements in reliability or efficiency

I've worked hard on this code and it functions exactly as I wanted, but I've been told it is a little bit unreadable. My focus is to get the code as clean as possible, and have things as efficient as possible.

This is the code I am working with: http://jsfiddle.net/xT5X5/6/

Can anyone give me any pointers on how I've done, and if there are any improvements that you can spot. Any feedback would be much appreciated!

Thank you.