Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / jQuery

jQuery UI Demystified: Part 2

5.00/5 (2 votes)
5 Oct 2013CPOL4 min read 7.6K  
In this post, we will learn about jQuery Interaction

Introduction

In this post, we will learn about jQuery Interaction. jQuery offers 5 kinds of interactions. All are mouse based and can be used in combination to provide easy interaction for users. This post will cover all the five jQuery UI interactions and it’ll use the source file of the Part 1 article. So if you haven’t had a look at Part 1, then you have two options. The first is you can continue with this article or you can have a look at Part 1.

Quick Overview of Part 1

In Part 1, we learned how to add a widget on any HTML element. In brief, it is like:

JavaScript
$(element_to_target).widget_name({List_of_options});

If we dismantle this line of code, then we have the following to learn:

  1. $ – It is a valid JavaScript identifier or variable name and it’s a shorthand notation for writing jQuery. For instance, the following two code samples represent the same thing.
    JavaScript
    code1:jQuery(‘.test’).tooltip();
    code2:$(‘.test’).tooltip();
  2. $(element_to_target) – Here, the mystery is in shorthand. If we replace the $ with jQuery, then it will look like this: jQuery(‘element_to_target’). Oh, guess what it is! It looks like nothing but a function call where "element_to_target" is a parameter passed to a function. In a nutshell, it represents a function call that wraps the element_to_target into a jQuery object.
  3. .widget_name() – It is used for setting a particular widget on that object.

For examples and more details, you can check Part 1 of this article.

Working with jQuery Interactions

Draggable

  • To add this effect, just add this line of code to your JS file after "$(‘#login’).tooltip({});": $(‘#dropme’).draggable({});

    Here, this line adds the draggable interaction to the accordion widget. Please note that excessive use of this interaction may lead to clustering of elements. So use it wisely.

  • Now add this in your HTML within the body tag but as a last element (it is not necessary, but helps in maintaining the page structure).

    JavaScript
    <div id=’dropme’ style=’width:100px;height:10px;color:red;’>Can you drop me on Green.</div>

    662948/a3p1.PNG

Droppable

Now add this line of code in your JS file to add droppable interaction on the newly created Div:

JavaScript
$("#plsCome").droppable({
    drop: function () {
        $(this).html("Thanks for your help!");
    }
});

This time, I added one option, "show", to the option braces to make the effect visible. This function will be executed when the element is dropped on it. The ".html()" sets the content of the div.

  • To add this effect, you need to add this code to your HTML file:

    JavaScript
    <div id=’plsCome’ style=’width:100px;height:100px;background-color:green;’></div>

    This basically adds a new div to the page that will be our drop target.

Resizeble

  • Want to change the size of an element? Then, add this line of code in your JS page after the line:

    JavaScript
    $(‘#dropme’).draggable({});
    $(‘#plsCome’).resizable({});

    This line will make your plsCome div a resizable div. Just try this option also "ghost:true".

    JavaScript
    $(‘#plsCome’).resizable({ghost:true});

    662948/a3p3.jpg

Selectable

  • Sometimes, it is necessary to make a selection visible in a list. For that, we can use selectable. For this, you need to add the following piece of code to your HTML page:

    HTML
    <style>
      #selectMe .ui-selected { background: #F39814; color: white; }
      </style>
      <br>
      <ul id="selectMe">
      <li class="ui-widget-content">Selection 1</li>
      <li class="ui-widget-content">Selection 2</li>
      <li class="ui-widget-content">Selection 3</li>
      </ul>

    Here, the style tag adds the CSS in the HTML page itself. The ".ui-selected" class is required. It applies the visuals after the selection is made. For other CSS classes that are available for this interaction, you can check the jQuery UI docs.

  • Now add this line of code to enable selection on the preceding list in your JS file:

    JavaScript
    $(‘#selectMe’).selectable({});

    662948/a3p4.PNG

Sortable

  • Here is the HTML:

    HTML
    <br>
    <ul id="sortMe">
    <li class="ui-widget-content">Sortable 2</li>
    <li class="ui-widget-content">Sortable 1</li>
    <li class="ui-widget-content">Sortable 3</li>
    </ul>
    
  • And as usual, the one-line jQuery in your JS file:

    JavaScript
    $(‘#sortMe’).sortable({});

Summary

All done. It’s time to check out what we have learned so far. We learned about jQuery Interaction. We uncovered the secrets behind $. (It’s a symbol that is important in real life as well as in web development so stick it into your brain). We covered all five of the interactions available in jQuery. We have seen how easy it is to use them. We saw examples of each interaction. In conclusion, it is important to understand when to use each interaction. If you use them wisely, then your page will definitely be blessed by jQuery. Don’t stop only on this now, you have the launch pad ready for diving into the jQuery Docs.

In Part 3, we will learn about the effects and how to use them in a simple and effective way.

Thanks for reading this article. Don’t forget to comment. Like is optional but good to have. :)

Live demo.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)