Introduction
In this article 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 article 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:
$(element_to_target).widget_name({List_of_options});
If we dismantle this line of code then we have the following to learn:
- $ - 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.
code1:jQuery('.test').tooltip();
code2:$('.test').tooltip();
-
$(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.
-
.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 off 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).
<div id='dropme' style='width:100px;height:10px;color:red;'>Can you drop me on Green.</div>
Droppable
-
To add this effect you need to add this code to your HTML file:
<div id='plsCome' style='width:100px;height:100px;background-color:green;'></div>
This basically adds a new div the to page that we will be our drop target.
-
Now add this line of code in your JS file to add droppable interaction on the newly created Div:
$("#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.
Resizeble
-
Want to change the size of an element? Then add this line of code in your JS page after the line: $('#dropme').draggable({});
$('#plsCome').resizable({});
This line will make your plsCome div a resizable div. Just try this option also "ghost:true".
$('#plsCome').resizable({ghost:true});
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:
<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:
$('#selectMe').selectable({});
Sortable
-
Here is the 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:
$('#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