Archive for February, 2009

Word count in textarea

I am showing an example for counting words in textarea. Its a very easy example and the most important part is the “countWords()” function. We are just observing the blur, keyup and focus events on textarea and when user write something in textarea the “words available” updates. You can also see its live demo here.

HTML:

<textarea rows=5cols=50id=”wc” name=”wc”>Hello World</textarea><br />
<span id=”status-ws”>100</span> words available

JavaScript:

<script type="text/javascript">
function updateWord(text, status, wordsAllow) {
	$(status).innerHTML = (+ (wordsAllow - countWords(text)));
}
 
function countWords(text) {
	show_word_count = true;
        var fullStr = text + ” “;
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
        var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, “”);
        var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
        var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, ” “);
        var splitString = cleanedStr.split(” “);
        var word_count = splitString.length -1;
 
        if (fullStr.length <2) {
    	      return word_count = 0;
        }
 
	return word_count;
}
 
updateWord($(’wc’).value, ’status-ws’, 100);
$(’wc’).observe(blur, function(oEvent){updateWord($(’wc’).value, ’status-ws’, 100);}, false);
$(’wc’).observe(”keyup”, function(oEvent){updateWord($(’wc’).value, ’status-ws’, 100);}, false);
$(’wc’).observe(focus, function(oEvent){updateWord($(’wc’).value, ’status-ws’, 100);}, false);
</script>

Live Demo


, ,

2 Comments


Select content from one list to another using jQuery

List Content Selection using jQuery

List Content Selection using jQuery

Few days back I was searching for some JavaScript functionality in which we can drag and drop elements from one list to another. I have found few good examples but all of them have some limitations. Like few are not working in IE or not fit for my requirement.

So, I have decided to write one myself. I have used jQuery JavaScript library because its provide some cool functions to easily work with DOM.

The example is easy to understand and you can easily extend or change it as per your need. I have tested it in Firefox 3.0, Google Chrome 1.0, Safari 3.3.2 and IE 7.0 on windows platform.

What I did is I have created two DIVs. First DIV have all the items when page load. User can select multiple items from first DIV using checkbox and click the right button to move the selected items from first DIV to second DIV. They can also move item from right DIV to left DIV using the left button.

You can view the working demo here.

You can download the source code here.

Here is the code for it with explaination:

HTML:

<div id=”all_users”>
	<div id=”user1″ userid=1class=”innertxt”>
        <img src=”images.jpg” width=75height=75″><strong>Test User 01</strong>
        <ul>
            <li>User ID: 1</li>
            <li>Email: user1@nodomain.com</li>
            <li style=”padding-top:5px;”><input type=”checkbox” id=”select1″ value=1class=”selectit” />
                <label for=”select1″>&nbsp;&nbsp;Select it.</label>
            </li>
        </ul>
    </div>
</div>
<div>
	<a href=”javascript:void(0);” id=”move_right”>Right &raquo;</a><br /><br />
	<a href=”javascript:void(0);” id=”move_left”>&laquo; Left</a>
</div>
 
<div id=”selected_users”></div>

We have two DIVs with ID “#all_users” and “#selected_users“. “#all_users” contains all the items and “#selected_users” is empty when page load. The first DIV contain an item”#user1“. We also have two links “#move_left” and “#move_right” for moving items between lists.

JavaScript:

<script type="text/javascript">
$(’#all_users .selectit).click(function() {
	var userid = $(this).val();
	$(’#user’ + userid).toggleClass(’innertxt_bg’);
});
</script>

When user check the “select it” checkbox of any item we add the “.innertxt_bg” class to that item. When user unchecked any “select it” checkbox we remove the “.innertxt_bg” class from that item. This is done by “toggleClass()” method. “toggleClass()” method first check the given class is assigned to element, if class is assigned then its remove it otherwise assign to element.

<script type="text/javascript">
$(”#move_right”).click(function() {
	var users = $(’#selected_users .innertxt2).size();
	var selected_users = $(’#all_users .innertxt_bg).size();if (users + selected_users > 5) {
		alert(’You can only chose maximum 5 users.’);
		return;
	}
 
    $(’#all_users .innertxt_bg).each(function() {
        var user_id = $(this).attr(’userid’);
        $(’#select’ + user_id).each(function() {this.checked = false;});
 
        var user_clone = $(this).clone(true);
        $(user_clone).removeClass(’innertxt’);
        $(user_clone).removeClass(’innertxt_bg’);
        $(user_clone).addClass(’innertxt2′);
 
        $(’#selected_users’).append(user_clone);
        $(this).remove();
	});
});
</script>

When user clicks the “move right” button, We check which items are selected in first list. All the selected item have the class “.innertxt_bg”. Then we create the clone of selected item using the jQuery “clone()” method and append this clone at the end of second list. After appending the clone to second list we remove the actual item from first list using the “remove()” method. We are also implementing the limit in selection of number of items from first list. We are using “size()” method to find the number of selected items.

<script type="text/javascript">
$(”#move_left”).click(function() {
	$(’#selected_users .innertxt_bg).each(function() {
        var user_id = $(this).attr(’userid’);
        $(’#select’ + user_id).each(function() {this.checked = false;});var user_clone = $(this).clone(true);
        $(user_clone).removeClass(’innertxt2′);
        $(user_clone).removeClass(’innertxt_bg’);
        $(user_clone).addClass(’innertxt’);
 
        $(’#all_users’).append(user_clone);
        $(this).remove();
	});
});
</script>

When user clicks the “move left” button, we just do exactly same what we did in case of “move right” click. The difference is we move item from right list to left list and doesn’t check the number of selected items as there is no need of it.

<script type="text/javascript">
$(’#view’).click(function() {
	var users =;
	$(’#selected_users .innertxt2).each(function() {
		var user_id = $(this).attr(’userid’);
		if (users ==)
			users += user_id;
		else
			users +=,+ user_id;
	});
	alert(users);
});
</script>

When user click the “view” button we will collect all the item IDs from the right list and display it to user using “alert()” method. You can send the selected item information to server using AJAX or you can assign it to a hidden field and submit the form.

You can view the working demo here.

You can download the source code here.


,

20 Comments


Search Result


No Comments