HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

You may have a situation where you want the user to choose from a list of elements. AJAX allows HTML5 and CSS3 programmers that option. The selectable widget is a great way to create this functionality from an ordinary list. The user can drag or Ctrl+click items to select them. Special CSS classes are automatically applied to indicate that the item is being considered for selecting or selected.

image0.jpg

Follow these steps to make a selectable element:

  1. Begin with an unordered list.

    Build a standard unordered list in your HTML. Give the ul an ID so that it can be identified as a jQuery node:

     <div id = "selectableTab">
      <h2 id="tab1" >selectable</h2>
      <ul id = "selectable">
      <li>alpha</li>
      <li>beta</li>
      <li>gamma</li>
      <li>delta</li>
      </ul>
     </div>
  2. Add CSS classes for selecting and selected states.

    If you want the selectable items to change appearance when the items are being selected or have been selected, add CSS classes as shown. Some special classes (ui-selecting and ui-selected) are predefined and will be added to the elements at the appropriate times:

     <style type = "text/css">
     h1 {
      text-align: center;
     }
     #selectable .ui-selecting {
      background-color: gray;
     }
     #selectable .ui-selected {
      background-color: black;
      color: white;
     }
     </style>
  3. In the init() function, specify the list as a selectable node.

    Use the standard jQuery syntax: selectable().

      $("#selectable").selectable();

The class is attached to all elements when they have been selected. Be sure to add some kind of CSS to this class, or you won't be able to tell that items have been selected.

If you want to do something with all the items that have been selected, just create a jQuery group of elements with the ui-selected class:

var selectedItems = $(".ui-selected");

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: