gidzior
gidzior

Reputation: 1805

i need optimize my code

i have trouble with optimization of the code below which is changing css property of some table cells after write or delete text from textarea

$('#par01par04text textarea').keyup(function() {
          var n = $(this).val().length;
          if (n > 0) {
            $('#par01TextComments.comments').css('display','table-cell');
            $('#par01LabelComments.comments').css('display','table-cell');
          }
          else if (n == 0){
            $('#par01TextComments.comments').css('display','none');
            $('#par01LabelComments.comments').css('display','none');
          }
        });


        $('#par02par04text textarea').keyup(function() {
          var n = $(this).val().length;
          if (n > 0) {
            $('#par02TextComments.comments').css('display','table-cell');
            $('#par02LabelComments.comments').css('display','table-cell');
          }
          else if (n == 0){
            $('#par02TextComments.comments').css('display','none');
            $('#par02LabelComments.comments').css('display','none');
          }
        });


        $('#par03par04text textarea').keyup(function() {
          var n = $(this).val().length;
          if (n > 0) {
            $('#par03TextComments.comments').css('display','table-cell');
            $('#par03LabelComments.comments').css('display','table-cell');
          }
          else if (n == 0){
            $('#par03TextComments.comments').css('display','none');
            $('#par03LabelComments.comments').css('display','none');
          }

i wrote this code but there is an error "$(textarea) is not defined" i know i'm close, could any one can help me put it together

$.each(
      [
        { textarea: '#pom02par01UwagiText textarea', Text: '#pom02par01TextComments.comments', Label: '#pom02par01LabelComments.comments' },
        { textarea: '#pom02par02UwagiText textarea', Text: '#pom02par02TextComments.comments', Label: '#pom02par02LabelComments.comments' },
        { textarea: '#pom02par03UwagiText textarea', Text: '#pom03par02TextComments.comments', Label: '#pom02par03LabelComments.comments' },
        { textarea: '#pom02par04UwagiText textarea', Text: '#pom02par03TextComments.comments', Label: '#pom02par04LabelComments.comments' }
      ],
      function(index,value){       
        $(textarea).keyup(function() {
          var n = $(textarea).val().length;
          if (n > 0) {
            $(value.Text).css('display','table-cell');
            $(value.Label).css('display','table-cell');
          }
          else if (n == 0){
            $(value.Text).css('display','none');
            $(value.Label).css('display','none');
          }
        });
      }
    );

Upvotes: 1

Views: 68

Answers (1)

mfeineis
mfeineis

Reputation: 2657

You have to select the current item:

...
function (index, value) {
    var me = $(value.textarea);
    me.keyup(function () { var n = me.val().length; ... });
}
...

Upvotes: 2

Related Questions