Page Content

Tutorials

Understanding the Interactions in jQuery With Example

Interactions in jQuery

Interactions in jQuery
Interactions in jQuery

The powerful jQuery UI interface components are designed to improve online programs by adding desktop app behaviours. These components let developers create complex UI interactions in jQuery without writing much JavaScript. They are among the jQuery UI widgets and plugins.

Sortable, Resizable, Droppable, and Draggable are main interactions in jQuery. Their behaviour adapts to needs, making them flexible. To utilise jQuery UI, you must include its JavaScript file, stylesheet, and graphics.

Draggable Component

Users can move an element around the page or even within another element by clicking and holding it with the mouse button with the Draggable component. This interaction is very helpful for designing interfaces that allow users to move windows, rearrange things, or engage with elements spatially.

Purpose: Allows for an element’s visual mobility by user input.

Application: The.draggable() method on the desired jQuery object is used to apply this behaviour. To make a div element draggable, for instance, your JavaScript code would look like $(‘#myDiv’).draggable();.

Configuration: Although the claim that draggable components are “highly configurable,” the snippets provided do not go into detail about draggable()’s particular configuration options, such as handles or constraints, beyond mentioning it in passing as having properties and options. But the main point is that you have the ability to make this behaviour active or inactive.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Draggable Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
  <style>
    #dragMe {
      width: 100px;
      height: 100px;
      background-color: orange;
      text-align: center;
      line-height: 100px;
      cursor: move;
    }
  </style>
</head>
<body>
<div id="dragMe">Drag me</div>
<script>
  $(function() {
    $("#dragMe").draggable();
  });
</script>
</body>
</html>

Output:

Drag me

Explanation:

The Draggable component lets web page elements be clicked and dragged with the mouse. The <div> element with ID dragMe is styled as a square box and made draggable using jQuery’s.draggable() method. The box can be clicked and moved freely in the browser window. This feature enhances user interaction for draggable windows, visual editor features, and layout rearrangement. The.draggable() method simplifies mouse event handling and makes the feature easy to create with one line of code.

Droppable Component

Draggable and the Droppable component function together. Where a draggable element can be “dropped” Draggable elements can modify appearance or call JavaScript methods when dropped on droppable elements.

Purpose: Setting a target location for draggable objects and how they react when dropped is the goal.

Application: Makes an element droppable. Use $(‘#dropArea’ ).id=”dropArea” should be droppable().

Configuration: Extracts say Droppable components are “highly configurable,” but they don’t elaborate like Draggable.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Droppable Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
  <style>
    #drag {
      width: 100px;
      height: 100px;
      background-color: lightgreen;
      cursor: move;
    }
    #drop {
      width: 150px;
      height: 150px;
      border: 2px dashed #444;
      margin-top: 20px;
    }
  </style>
</head>
<body>
<div id="drag">Drag</div>
<div id="drop">Drop here</div>
<script>
  $(function() {
    $("#drag").draggable();
    $("#drop").droppable({
      drop: function(event, ui) {
        $(this).css("background-color", "lightblue").html("Dropped!");
      }
    });
  });
</script>
</body>
</html>

Output:

Drag
Dropped!

Explanation:

The Droppable component outlines a page region for dropping draggable items. Using.droppable(), a green box labelled “Drag” and a larger dashed box labelled “Drop here” are made draggable and droppable, respectively. Release of the draggable box over the droppable area changes the drop area’s background colour and text to “Dropped!” This shows how dynamic and intuitive elements can interact, allowing developers to design drag-and-drop file upload zones, interactive lists, or UI workflows that require dragging objects into categories or containers.

Resizable Component

With the Resizable component, users may click and drag any element’s corners to modify its size. Interfaces that need to quickly change content sections, visual displays, or unique user interface elements can benefit from this interaction.

Purpose: Offers a user-friendly method for manually modifying an element’s dimensions.

Application: This interaction is applied by using a jQuery object’s.resizable() method.

Configuration: “Highly configurable” refers to resizing. Handles is a crucial configuration option that is mentioned since it enables you to limit the resizing. By default, for instance, resizable() places a resizing handle at the element’s lower-right corner. You can, however, set handles to’s’ (for “south” or bottom) to ensure that resizing only occurs vertically.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Resizable Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
  <style>
    #resizeBox {
      width: 150px;
      height: 150px;
      background-color: pink;
      padding: 10px;
    }
  </style>
</head>
<body>
<div id="resizeBox">Resize me</div>
<script>
  $(function() {
    $("#resizeBox").resizable({
      handles: "se"  // Only southeast (bottom-right) corner
    });
  });
</script>
</body>
</html>

Output:

Resize me

Explanation:

The Resizable component lets users manually resize elements by clicking and dragging their corners. We use the.resizable() function to resize a red box labelled “Resize me” and the handles: “se” option to restrict the handle to the southeast (bottom-right) corner. Resizable picture previews, text boxes, and dashboard widgets benefit from this interaction. Developers can enable users modify component size without manually calculating dimensions or adding mouse event logic by using.resizable().

Sortable Component

Sortable allows grid and list reordering by drag and drop. Playlists, task lists, and content management systems are examples of dynamic interfaces where users must alter the order of elements.

Purpose: Enables drag-and-drop interactive rearranging of list items or other components.

Application: By using the.sortable() method on a container element, you may make this behaviour active. To make the list elements in #myList sortable, for instance, use $(‘#myList’).sortable();.

Configuration: The emphasise how “highly configurable” Sortable components are, enabling developers to specify features like axis limitations, containment, and the look of placeholders.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>Sortable Example</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
  <style>
    #sortable li {
      list-style: none;
      margin: 5px;
      padding: 10px;
      background-color: #e0e0e0;
      cursor: move;
    }
  </style>
</head>
<body>
<ul id="sortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
<script>
  $(function() {
    $("#sortable").sortable();
  });
</script>
</body>
</html>

Output:

Item 1
Item 2
Item 3

Explanation:

Users can drag and drop items in a list or group using the Sortable component. To make an unordered list (<ul>) with three items sortable, use the.sortable() method. After activation, users can drag items up or down to adjust their list order. Task managers, playlist editors, and other interfaces that allow item ordering employ this capability. The.sortable() method simplifies drag-and-drop sorting and makes interfaces more interactive and personalised, improving user engagement.

General Benefits and Considerations

Utilising the interaction elements of jQuery UI has the following benefits:

Simplified Development: They remove the intricate details that vary depending on the browser when implementing these interactive behaviours in standard JavaScript. “Write less, do more” is what this means for developers.

Consistency: Even for a large library, jQuery UI strives for uniformity across all of its features, making learning and application easier.

Enhanced User Experience: These elements greatly increase the responsiveness and usability of web apps, giving them a more dynamic and captivating feel by offering recognisable and simple desktop-like interactions in jQuery.

Extensibility:These interactions in jQuery are designed to be customisable and enhanced as part of the jQuery plugin architecture.

Cross-Browser Compatibility:Query and jQuery UI automatically fix “cross-browser incompatibilities,” satisfying users.

In conclusion, the interactions in jQuery elements, such as Sortable, Droppable, Resizable, and Draggable, offer strong, readily usable answers to typical web interface problems. They let developers to quickly and effectively create dynamic web front-ends, turning static websites into dynamic apps.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index