jQuery UI Demystified: Part 1

Introduction

 
In this tutorial, we will learn about the jQuery UI developed using JavaScript. The UI in jQuery UI stands for User Interface which means that it's a library to provide an interface to the user to interact with your web application or web site. It is a kind of abstraction provided to the user for doing low-level interaction and designing. To survive in the current technology web designers must learn jQuery. jQuery makes website design for anyone a cakewalk. This library contains 11 widgets, five interactions, and nine effects. This tutorial covers the 3 widgets and all the interactions and effects of each of the widgets. Let's first see what a Widget is.
 

Widgets in jQuery UI

 
Widgets are just like any other JavaScript object attached to a Web Element to maintain its interaction, lifetime, theming, state and so on. The jQuery plugins are used as simply as other jQuery methods. Widgets are very helpful in web design. They save many of your keystrokes wasted by writing the code from scratch to get the same functionality. That's enough for Widgets.
 
Some jQuery Widgets
 

Accordion

 
The accordion is a jQuery widget that helps you to organize your web content into content boxes. If you have less space in a web page and more data to show then Accordion is what you are looking for. It can show/hide the data as per the user's needs.
 

Dialog

 
A Dialog is used to show the data in a pop-up box. A pop-up box is nothing but an elevated data container that gives the user a feel that something is happening. If you want something that catches the user's eye then choose the Dialog Widget.
 

Tooltip

 
A Tooltip is used to show hints and directions to users. If you want your website to be very user-friendly then you must give it a chance.
 

Interaction in jQuery UI

 
Interaction in jQuery UI is a way to provide interaction between your web element and the user. The user can interact with web elements in many ways but as a designer, you must know which interaction is the most suitable. In jQuery, we have five types of interactions. Each has its own uses and advantages.    
 
jQuery Interactions
 

Draggable

 
Draggable provides drag functionality to the element. If you want something whose location is to be arranged by the user or moved by the user then it is what you can look for.
 

Droppable

 
Droppable is related to Draggable. It provides a target for draggable elements. For example in the game of football, the ball is draggable and the Goal Post is droppable.    
 

Resizable

 
It allows the user to resize the element. Very useful if you want to allow the user to resize the element.
 

Selectable

 
Want to show what use has been selected in the list? Then this is what you can use. It allows an element to be selectable.
 

Sortable

 
Allow arranging or sorting of Items. Useful when item reordering is required.
 

Effects in jQuery UI

 
Effects are nothing but everything for any good website. Whatever magic you see on websites are mostly using the jQuery effects. It applies an animation on an element. In this tutorial, we will learn Hide, Show and toggle class effects.     
 

Working With jQuery

 
Part 1 Adding widgets on a web page
 
Accordion
  1. Create an empty web page with basic HTML in it and put in the Body:
    1. <div id='accordion'>  
    2.    <h3>Title 1</h3>  
    3.    <div>  
    4.       Content 1 Goes here.  
    5.    </div>  
    6.   <h3>Title 2</h3>  
    7.    <div>  
    8.       Content 2 Goes here.  
    9.    </div></div> 
  2. Create a JS file and add this code:
    1. $(document).ready(function () {    
    2.     $("#accordion").accordion({});    
    3. });   
    Here the second line adds the Accordion Widget to the div having the ID "accordion". You can use any widget option, like Active, animate, collapsible and so on inside the {} curly brace. For example: "{Active:2}".
     
    pic1a2.PNG
     

Dialog

  1. In your HTML file put this code in the Body:
    1. <inputtypeinputtype='button'value="Login"id="login"/>  
    2. <dividdivid='loginDlg'style="display:none;">  
    3.    Username:<inputtypeinputtype='text'/><br>  
    4.    Password:<inputtypeinputtype='password'/>  
    5. </div>
  2. In your JS file add this code after line 3:
    1. $('#login').click(function () {  
    2.     $("#loginDlg").dialog({});  
    3.  }); 
    Here line 5 will open the "Login as" Dialog when the user clicks on the Login button. Try adding this code between the curly braces and check the Output :)
    1. show: {                     
    2. effect:"blind",  
    3. duration: 1000  
    4. },  
    5. hide: {  
    6.   effect:"fold",  
    7.   duration: 1000  
    8. }  
     
    pic2a2.PNG
     

ToolTip

  1. Now add the title to your login button. The code will be like this:
    1. <inputtype='button'value="Login"id="login"title="Click this button to open login Dialog "/> 
  2. In your JS file add this line:
    1. $('#login').tooltip({});  
    Here Line 7 adds the tool tip to the login button. 
     
    pic3a2.PNG.jpg
     
     Live Demo Here

Summary 

 
It's time to summarize what we have learned so far. We learned about the jQuery widgets. We saw how easy it is to add a widget to any element. We saw how 2 lines of jQuery give us this versatile and magnificent view of the site. As a takeaway of this article you can remember this rule of thumb for adding any widget to an element, use this syntax:
 
$(element_to_target).widget_name({List_of_options});
 
The code is kept simple and short for the sake of understanding the concept. In Part 2, we will learn about jQuery Interactions and how to use them properly. So stay tuned and don't forget to Comment.  


Recommended Ebook

Voice of a Developer: JavaScript From Scratch

Download Now!
Similar Articles