Home

How to Make Selectable Elements in AJAX for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:13:27
|
HTML5 and CSS3 All-in-One For Dummies
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 earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].