Recent News

Interactive character limit for textarea using Jquery

Posted by admin on November 9th, 2007

For a textbox, character of a field can easily be limited with maxlength attribute. But maxlength doesn’t work for  a textarea. So, It needs an alternate way. For one of my project, I wrote a function for this purpose where I used jQuery. Here I am explaining how I did it.

Let’s look an example of this interactive solution here

OK. Now we will make this in 2 easy steps.

1. Import your jQuery file and write the function "limitChars(textid, limit, infodiv)" in the head section of your page.

This function takes 3 parameters. They are:

  • textid : (string) The ID of your textarea.
  • limit : (num) The number of character you allow to write.
  • infodiv : (string) The ID of a div, in which limit information will be shown .

  Here is the function:

 1: <script language="javascript" src="Jquery.js"></script>
 2: <script language="javascript">
 3: function limitChars(textid, limit, infodiv)
 4: {
 5: var text = $(‘#’+textid).val(); 
 6: var textlength = text.length;
 7: if(textlength > limit)
 8: {
 9: $(‘#’ + infodiv).html(‘You cannot write more then ‘+limit+‘ characters!’);
 10: $(‘#’+textid).val(text.substr(0,limit));
 11: return false;
 12: }
 13: else
 14: {
 15: $(‘#’ + infodiv).html(‘You have ‘+ (limit - textlength) +‘ characters left.’);
 16: return true;
 17: }
 18: }
 19: </script>

Remember, the script is using jQuery. So be careful about the path of jquery at first line of this script.
 
2. Bind the function to the keyup event of your textarea. Do this in jQuery’s ready event of document. Just like this:
 1: $(function(){
 2: $(‘#comment’).keyup(function(){
 3: limitChars(‘comment’, 20, ‘charlimitinfo’);
 4: })
 5: });

Here my textarea’s id is ‘comment’ , limit of characters is 20 and limit information will shown in a div whose id is ‘charlimitinfo’. That’s all, we have made an "Interactive character limiter" for our textarea using Jquery.
 
If you are not using jQuery for your site, it will not be wise to include it only for this script. You can get the same functionality from javascript without jquery. Look at this example. It looks like and works 100% as the previous though not using jquery.
Here you need just 2 changes. First, change the function "limitChars" as follows :
 1: function limitChars(textarea, limit, infodiv)
 2: {
 3: var text = textarea.value; 
 4: var textlength = text.length;
 5: var info = document.getElementById(infodiv);
 6:  
 7: if(textlength > limit)
 8: {
 9: info.innerHTML = ‘You cannot write more then ‘+limit+‘ characters!’;
 10: textarea.value = text.substr(0,limit);
 11: return false;
 12: }
 13: else
 14: {
 15: info.innerHTML = ‘You have ‘+ (limit - textlength) +‘ characters left.’;
 16: return true;
 17: }
 18: }

 
And finally, call the function manually on keyup event of your textarea. Use "this" keyword for textarea instead of id when calling the function. example:
 1: <textarea name="comment" id="comment" onkeyup="limitChars(this, 20, ‘charlimitinfo’)">
 2: </textarea>

Finished. We made the solution again without dependency of any javascript framework.

Please let me know if you have an idea to make it better. 

jQuery controlled Dependent (or Cascading) Select List 2

Posted by admin on November 8th, 2007

Thank you all. Thanx for using, commenting and visiting my script jQuery controlled Dependent (or Cascading) Select List from my old bolg http://php4bd.wordpress.com and here.

I am getting a lots of request and questions from visitors about adding some features and some other problems. Sometimes I faced some problem when using this script myself. So, I have made some changes here. I hope, this change will help you to overcome some problems of previous version of this script. The main features added in this version are:

  1. It will keep in child list box only sub items of selected parent value when initializing.
  2. Added a 4th parameter to input a selected value for child list box.
  3. When "isSubselectOptional" is true, add a default value ‘none’ for ‘– Select –’ option of child list box.
  4. Automatically focus the child list box when a value is selected from parent list box.
  5. More effective for multilevel association.

Click here to see a demo of multilevel association using this function.

Here is the modified function:

 1: function makeSublist(parent,child,isSubselectOptional,childVal)
 2: {
 3: $("body").append("<select style=’display:none’ id=’"+parent+child+"’></select>");
 4: $(‘#’+parent+child).html($("#"+child+" option"));
 5: 
 6: var parentValue = $(‘#’+parent).attr(‘value’);
 7: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
 8: 
 9: childVal = (typeof childVal == "undefined")? "" : childVal ;
 10: $("#"+child+‘ option[@value="’+ childVal +‘"]’).attr(’selected’,’selected’);
 11: 
 12: $(‘#’+parent).change( 
 13: function()
 14: {
 15: var parentValue = $(‘#’+parent).attr(‘value’);
 16: $(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
 17: if(isSubselectOptional) 
 18: $(‘#’+child).prepend("<option value=’none’> — Select — </option>");
 19: $(‘#’+child).trigger("change"); 
 20: $(‘#’+child).focus();
 21: }
 22: );
 23: }

And initialize the association on ‘$(document).ready’, as following example:

 1: $(document).ready(function()
 2: {
 3: makeSublist(’parentID’,‘childID’, true, ‘selected_val_of_child’);
 4: });

If you want to create multilevel association, start the initialization from child most order. such as:

 1: $(document).ready(function()
 2: {
 3: makeSublist(‘child’,‘grandson’, true, ); 
 4: makeSublist(‘parent’,‘child’, false, ‘3′); 
 5: });

For details instruction and example of using this script, please visit the previous version. Feel free to ask me if any problem with using this script or any bug you found.

Recent Comments | Recent Posts


designed by: Website Builder | Coded by: Blog Directory | Provided by: Wedding photojournalism chicago
bottom