views:

67

answers:

4

Hello,

I've included code for my textbox/textarea counter I put together. (hope it doesn't get mangled in my post...)

The script works quite well, however I know my code is ridiculously bloated due to my inexperience with jquery and javascript.

I was hoping someone could show me how to take the repetitive code (//event one thru five) and put it into its own function, then call that function on each event.

Thanks,

Jeff

$(document).ready(function() {


if ($('span[class=tbc] input[type=text], span[class=tbc] textarea')) {        
    $('span[class=tbc] input[type=text], span[class=tbc] textarea').each(function() {

       var maxL = $(this).attr('MaxLength');

        $(this).siblings('span[class=tbc]').prepend('<br /><span id="spanID1">Characters remaining: </span>'); 
        $(this).siblings('span[class=tbc]').append('<strong>' + maxL + '</strong>');


            //event one
            $(this).keyup(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end keyup function  


            //event two
            $(this).keydown(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end keydown function  


            //event three
            $(this).mouseout(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end mouseout function 


            //event four
            $(this).mousemove(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end mousemove function 


            //event five
            $(this).blur(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end blur function 


    });  //end each loop
} //end if ($('span[class=tbc] input[type=text] 

}); //end ready
A: 

Here is an example of what it looks like you're trying to achieve:

http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial

It shows how to go from what you have to a plugin.

Jim Schubert
A: 
var handler = function() {
        var cnt = $(this).val().length;
        var chars = maxL-cnt;
        var mesg = $(this).parent('span').attr('ID');

        $(this).siblings('span[class=tbc]').html('
Characters remaining: ' + chars + ''); if (cnt > maxL - 1) { //alert('Cannot enter anymore characters.'); $(this).siblings('span[class=tbc]').html('
Characters remaining: ' + chars + ' (Cannot enter anymore characters.)'); } }; $(this).keyup(handler); $(this).keydown(handler); etc.
Ben McCann
Thanks for the info. It seems so simple now... :-)
Jeff
A: 

You can use the bind method in jQuery to bind a list of events instead of binding them one by one:

$(document).ready(function() {

    // No need for if since .each() will do nothing on an empty jQuery    
    $('span[class=tbc] input[type=text], span[class=tbc] textarea').each(function() {

       var maxL = $(this).attr('MaxLength');

       // Try to save queries that you use a lot
       var $tbc = $(this).siblings('span[class=tbc]');

       // You can use append() and prepend() in the same jQuery chain
       $tbc.prepend('<br /><span id="spanID1">Characters remaining: </span>')
           .append('<strong>' + maxL + '</strong>');

            // Bind all events at once
            $(this).bind("keyup keydown mouseout mousemove blur", function() {
                var cnt = $(this).val().length;
                var chars = maxL-cnt;
                var mesg = $(this).parent('span').attr('ID');

                $tbc.html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

                if (cnt > maxL - 1) {   
                    //alert('Cannot enter anymore characters.');
                    $tbc.html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
                }
            }); //end bind


    });  //end each loop

}); //end ready

I tweaked your code a little more so you can learn some jQuery tricks.

Vincent Robert
Thanks so much for your help. This is a much more elegant and refined version compared to my heaping pile of code. ;-)I really appreciate your included comments as this helps me better understand what's going on.Thanks again,Jeff
Jeff
A: 
$(document).ready(function() {
    $('span.tbc').find('input[type=text], span.tbc textarea').each(function() {
     var ele = $(this);
     var maxL = ele.attr('MaxLength');
     var siblings = ele.siblings('span.tbc');
     siblings
      .prepend('<br /><span id="spanID1">Characters remaining: </span>')
      .append('<strong>' + maxL + '</strong>');
     //event one-five
     ele.bind("keyup keydown mouseout blur mousemove", function() {
      var cnt = ele.val().length;
      var chars = maxL-cnt;
      var mesg = ele.parent('span').attr('ID');
      if (cnt > maxL - 1) {
       //alert('Cannot enter anymore characters.');
       siblings.html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>');
      } else {
       siblings.html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');
      }
     }); //end event function
    });  //end each loop
});
jitter
Thanks for the help. I didn't think about adding the if else statement as you have in this code. Nice work.
Jeff
No problem. BTW, I forgot that you will probably need to bind the function to the 'this' context that you want.
Ben McCann