Navigating the JQueryUI Sortable Feature with TypeScript

Using the JQueryUI Sortable (version 1.12.1) method in a TypeScript (version 3.2.1) environment has mostly been smooth sailing for me. However, I've hit a roadblock while trying to implement the Sortable Widget's helper option. Here's a snippet of my TypeScript code:

$('.connected-sortable').sortable({
    connectWith: '.connected-sortable',
    delay: 150,
    helper: (evt: Event, item: JQueryUI.Sortable) => {
        // How can I interact with the JQueryUI.Sortable object?
        // I need access to a JQuery<HTMLElement> or just an 
        // Element to manipulate.

        // The following attempt fails:
        // Property 'hasClass' does not exist on type 'Sortable'
        item.addClass('selected');
    }
});

I'm struggling to treat the JQueryUI.Sortable object like a JQuery<HTMLElment> or even a plain HTML Element. My JavaScript version of this code is based on this Fiddle.

If anyone could offer guidance on how to achieve this, I'd greatly appreciate it. Thank you.

Answer №1

To access the sortable item, you can use ui.item.

In the sortable's start function, you have the option to add a class, and in the stop function, you can remove that class.

$('.connected-sortable').sortable({
  connectWith: '.connected-sortable',
  delay: 150,
  helper: (evt: Event, item: JQueryUI.Sortable),
  start: function(event,ui) {
    ui.item.addClass('selected');
  },
  stop: function(event,ui) {
    ui.item.removeClass('selected');
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating the x and y coordinates for drawImage in HTML5

Understanding the x and y axis has posed a challenge for me: //retrieve last known x and y coordinates ...code var p = $("#draggable"); var offset = p.offset(); var x = offset.left; var y = offset.top; //establish new font siz ...

Selecting CSS Properties with jQuery

I am trying to make an image change color to blue and also change the background color to blue when it is clicked inside a div. However, my code doesn't seem to be working as expected. Is there a more efficient way to apply CSS to elements? FIDDLE v ...

Tips for utilizing jQuery ajax to invoke a PHP function

I have a file named myfunctions.php where I keep a collection of functions, such as function submitForm(){ //save form data } function anotherFunction(){ //perform a task } // More functions ... and the jQuery script, $.ajax({ url: "myfunction ...

Practical Tip for jQuery - Storing DOM Element in a Variable for Consistent Usage Across Your Codebase

As I was searching for some Jquery best practices, I came across this gem. Source: // Storing the live DOM element in a variable var elem = $("#elem"); // Setting an element's title attribute using its current text elem.attr("title", elem.te ...

In order to retrieve specific object attributes using the unique identifier

I am currently managing 2 API's referred to as teachers and sessions. The contents of the teachers JSON file are: [ { "teacherName": "Binky Alderwick", "id": "01" }, { "teacherName": "Basilio Gregg", ...

Issues encountered when trying to access a POST endpoint with .NET Framework 4.6.2

I have developed a WEB API using .NET Framework 4.6.2, specifically in my AuthController where I have implemented a test endpoint that requires a parameter. Unfortunately, I am encountering issues in hitting this endpoint and receiving the following error ...

Retrieve the text from the outer span excluding any text from the inner elements

I am looking to retrieve specific text from an HTML element using Selenium and Javascript. <span class="myAttribute"> <b>SomeValueToIgnore</b> <span class="ignoreWhatsInside"> <em>ignore_that</em> </span& ...

Activate JavaScript functions by pressing the enter key, allowing for various searches, AJAX requests, and DataTable displays to occur seamlessly without the need to refresh

I recently developed a web page that integrates an AWS API interface to interact with an RDS Aurora MySQL Serverless database. Users can input a SQL statement and click the Query button, which triggers an AJAX request, returns JSON data, and converts the d ...

Using JQUERY to handle event bubbling in the DOM after an AJAX call

I have been attempting to dynamically append content to a specific div using .ajax() and then utilize the new content within a jQuery Dialog. I believe that my usage of .on() is correct, but it appears that the DOM is not being properly updated. Here is t ...

What is the reason for a boolean extracted from a union type showing that it is not equivalent to true?

I'm facing a general understanding issue with this problem. While it seems to stem from material-ui, I suspect it's actually more of a typescript issue in general. Despite my attempts, I couldn't replicate the problem with my own types, so I ...

Leveraging local resources to create images with the help of @vercel/og and Next.js

Utilizing the latest @vercel/og library for generating meta-tag images has been quite intriguing. The official example showcases how to leverage images from an external source. The Quandary at Hand <img src={"https://res.cloudinary.com/iqfareez ...

Having difficulty receiving a response from an AJAX call within the success function

After browsing through this stack link Ajax response not calling success:function() when using jQuery 1.8.3, I'm puzzled as to why the success function is not invoked when I uncomment the dataType line. It seems that setting dataType = JSON prevents t ...

The functionality to refresh an input field upon clicking a button is not functioning as expected

I am currently developing a small MVC.NET web application with user input functionality. The goal is to calculate and display the results in two input fields upon button click. However, I am facing an issue where the input fields remain empty after the but ...

What could be the reason for the esm loader not recognizing my import?

Running a small express server and encountering an issue in my bin/www.ts where I import my app.ts file like so: import app from '../app'; After building the project into JavaScript using: tsc --project ./ and running it with nodemon ./build/bin ...

How to use AngularJS to collapse various panels with unique content

Hey everyone, I'm working on developing a collapsible panel using Angular. The panel should consist of a header and body to display the content. The desired behavior is that when a button is clicked, the content collapses down, and clicking the same b ...

The error message "vimeo/player.js - Unable to access property 'nativeElement' as it is undefined" appeared

I am encountering difficulties integrating vimeo/player.js into my angular-cli application. There isn't a supporting library for Angular 4, so I'm following the steps in the README.md under "Using with a module bundler". I created a vimeo-player ...

On paste events, CKEditor will strip away all HTML tags except for div and span elements

I am trying to implement a feature in CKeditor where all HTML tags are removed when a user pastes content using Ctrl+v. The code I have written so far does not seem to work as expected. <script type="text/javascript"> CKEDITOR.on('instanceR ...

What is the process for transferring a saved index to a different div element?

Having an issue with my HTML structure where I have hidden div elements and trying to show them on click of a paragraph tag. I want to store the index of the clicked paragraph and use it to display the corresponding hidden div with the same index. Howeve ...

What steps are involved in setting up a RESTful service for a web application?

After extensive searching, I'm still unclear on how to approach the question posed in the title. The concept of RESTful was introduced to me just this morning, and the more I delve into it, the more puzzled I become. A few weeks back, I undertook the ...

What is the best way to verify a date in the format (yyyy-mm-dd) using jQuery?

I'm in the process of validating a date with the format (yyyy-mm-dd). I came across a solution, but it's not quite what I need as it's in the format (mm/dd/yyyy). You can check out the solution here: http://jsfiddle.net/ravi1989/EywSP/848/ ...