Posts Tagged listbox
JavaScript List Search using jQuery
Posted by admin in JavaScript, jQuery on October 30th, 2009
Here in this example we are using jQuery to filter the list of products. We have list of products in UL and LI. We have associated the “KeyUp” event on the top search textbox. So when somebody type in the textbox we filter the list on the basis of entered value.
We are using the RegExp for comparing the string for matching characters. First we created the regular expression “rg” from the given text and compare the product name (string) with the help of search function.
The search function return the position of string where match is found or ‘-1′ in case of no match.
So, we selected all the LI’s exist inside the ‘product_list‘ ID and match each product name one by one. We hide the LI if its text not match the current search text and display the LIs with matching text.
HTML Code: -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sales Vu</title> <link rel="stylesheet" href="pm-common-new.css" type="text/css" /> <!--[if IE]><link rel="stylesheet" href="ie.css" type="text/css" media="screen, projection"><![endif]--> </head> <body style="padding-left:20px;"> <div class="top-bar"> <div class="top-left-corner"><img src="images/top-left-corner.png" /></div> <div class="top-mid-bg">Product Management</div> <div class="top-right-corner"><img src="images/top-right-corner.png" /></div> </div> <div class="body-part"> <div class="body-part-top"> <div class="search-container"> <div class="search-box" style="float:left; width:257px;"> <div style="float: left; width: 70px; padding-top: 4px; font-weight: bold;">Search </div> <div class="text-field-box"> <input name="" type="text" id="search" autocomplete="off" class="search-box-bg" /> </div> <div class="text-field-cancel-button"> <a href="javascript:void(0);" id="search_clear" ><img src="images/search-box-cancel.png" border="0" id="search_clear" /></a> </div> </div> </div> <div class="modifier-product"> <div class="modifier-product-right" style="float:left;"> <div class="modifier-product-right-top"> Product </div> <div class="modifier-product-right-mid"> <div class="modifier-product-right-detail" id="product_list"> <div class="modifier-product-right-detail-heading"> <div class="product-list-heading">Product</div> <div class="price-list-heading">Price ($)</div> </div> <div class="product-list"> <ul> <li> <div class="product">Banana Split</div> <div class="price">3.00</div> </li> <li> <div class="product">Bird Lite</div> <div class="price">2.45</div> </li> <li> <div class="product">Butter</div> <div class="price">2.00</div> </li> <li> <div class="product">Chicken With Coke</div> <div class="price">3.45</div> </li> <li> <div class="product">Chips With Coke</div> <div class="price">3.75</div> </li> <li> <div class="product">Corono Lite</div> <div class="price">2.00</div> </li> <li> <div class="product">Eggs</div> <div class="price">0.50</div> </li> <li> <div class="product">Limca</div> <div class="price">1.05</div> </li> <li> <div class="product">Sprite</div> <div class="price">1.20</div> </li> <li> <div class="product">test product</div> <div class="price">3.00</div> </li> </ul> </div> </div> </div> <div class="modifier-product-right-bottom"> </div> </div> </div> </div> <div class="body-part-bottom"> <img src="images/body-bottom.png" /> </div> </div> </body> </html> <script src="jquery.js" type="text/javascript"></script> <script src="pm_new.js" type="text/javascript"></script>
JavaScript Code: -
// JavaScript Document $(document).ready(function () { $('#search').keyup(function(event) { var search_text = $('#search').val(); var rg = new RegExp(search_text,'i'); $('#product_list .product-list .product').each(function(){ if($.trim($(this).html()).search(rg) == -1) { $(this).parent().css('display', 'none'); $(this).css('display', 'none'); $(this).next().css('display', 'none'); $(this).next().next().css('display', 'none'); } else { $(this).parent().css('display', ''); $(this).css('display', ''); $(this).next().css('display', ''); $(this).next().next().css('display', ''); } }); }); }); $('#search_clear').click(function() { $('#search').val(''); $('#product_list .product-list .product').each(function(){ $(this).parent().css('display', ''); $(this).css('display', ''); $(this).next().css('display', ''); $(this).next().next().css('display', ''); }); });
Select content from one list to another using jQuery
Posted by admin in JavaScript, jQuery on February 27th, 2009

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=”1″ class=”innertxt”> <img src=”images.jpg” width=”75″ height=”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=”1″ class=”selectit” /> <label for=”select1″> Select it.</label> </li> </ul> </div> </div> <div> <a href=”javascript:void(0);” id=”move_right”>Right »</a><br /><br /> <a href=”javascript:void(0);” id=”move_left”>« 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.
