Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to create To-Do List using jQuery?
Next article icon

How to create Dynamic Drag and Drop table rows using jQuery Ajax?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss the method of How to create Dynamic and Drop table rows using JQuery Ajax.  We will use jQuery and HTML CSS to perform the operations of Drag and Drop on the table row.

First to perform the operations of the Drag and Drop table row we need two jQuery libraries without these libraries we can't perform the Drag and Drop operations on the table row. The required two libraries are:

  • jQuery Ajax min.js: It will help us to perform the Drag and Drop operation on the table row:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

  • jQuery-UI: This as the name suggests, it's used for showing user interface operations:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

Basically Drag and Drop operations on table rows means we can interchange the table row with each other by only operating Drag and Drop operations we need to only Drag and one row Drop it on another row with which we want to perform Drag and Drop operations it's basically used for change data and for the correctness of data when we write or add wrong data on a table.

Approach: First we create table by using table and td , tr tag in our html code and we add one h1 tag for heading through which i explained what we are making and in first tag or tr we add three column first for id second, language and last one for preference and we add id 1 to 8 and in language we write some coding  language and at last we are adding preference which we will set stable it means when we will perform Drag and Drop operations so that preference will not change their sequences its always static 1 to 8 only two column / row change id and preference according to perform operation by user and after writing code for table we add jQuery libraries without which we can't operate Drag and Drop operations and the we give id in table GFG and at last we write js first we create one function in which we will use GFG id and we will use sortable method and we use droponEmpty as a false default and after we start operation by using one more function and we addclass select and for stop the operation we will make one other function and set the stop function and write code for stop and successful showing interchange table operations on table row and we set GFG greater than 0 which and use findmethod in which we use td for Drag and Drop operations so with the help of these code we can perform Drag and Drop operations on table row.

Example: This is the implementation of the above approach:

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Dynamic Drag and Drop table rows using jQuery Ajax</title>     <style>         body {             display: flex;             flex-direction: column;             align-items: center;         }          table th,         table td {             width: 50px;             padding: 5px;             border: 0.5px solid black;         }          .GFG {             color: green;         }          .OK {             font-size: 18px;         }     </style>     <script type="text/javascript"         	src= "https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">   	</script>     <link rel="stylesheet"           href= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />     <script type="text/javascript" 	        src= "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js">   	</script>     <script type="text/javascript"     	    src= "https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js">   	</script> </head>   <body>     <h2 class="GFG">GeeksForGeeks</h2>     <p class="OK">Drag and Drop table row</p>     <div>         <table id="GFG">             <tr>                 <td>ID</td>                 <th>Language</th>                 <th>Preference</th>             </tr>             <tr>                 <td>1</td>                 <td>Java</td>                 <td>1</td>             </tr>             <tr>                 <td>2</td>                 <td>C++</td>                 <td>2</td>             </tr>             <tr>                 <td>3</td>                 <td>Python</td>                 <td>3</td>             </tr>             <tr>                 <td>4</td>                 <td>HTML</td>                 <td>4</td>             </tr>             <tr>                 <td>5</td>                 <td>CSS</td>                 <td>5</td>             </tr>             <tr>                 <td>6</td>                 <td>Javascript</td>                 <td>6</td>             </tr>             <tr>                 <td>7</td>                 <td>MySql</td>                 <td>7</td>             </tr>             <tr>                 <td>8</td>                 <td>PHP</td>                 <td>8</td>             </tr>         </table>     </div>      <script>         $(function () {             $("#GFG").sortable({                 items: 'tr:not(tr:first-child)',                 dropOnEmpty: false,                 start: function (G, ui) {                     ui.item.addClass("select");                 },                 stop: function (G, ui) {                     ui.item.removeClass("select");                     $(this).find("tr").each(function (GFG) {                         if (GFG > 0) {                             $(this).find("td").eq(2).html(GFG);                         }                     });                 }             });         });     </script>  </body>  </html> 

Output:


Next Article
How to create To-Do List using jQuery?
author
prathamsahani0368
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • JQuery
  • Technical Scripter 2022
  • jQuery-AJAX
  • jQuery-Questions

Similar Reads

  • How to Dynamically Add/Remove Table Rows using jQuery?
    We will dynamically add/remove rows from an HTML table using jQuery. jQuery provides us with a lot of methods to perform various tasks. To dynamically add and remove the rows from an HTML table, we are also going to use some of these jQuery methods like append(), remove(), etc. Adding a rowTo add a
    3 min read
  • How to create a drag and drop feature for reordering the images using HTML CSS and jQueryUI ?
    In this article, we are given an images gallery and the task is to re-arrange the order of images in the list or grid by dragging and dropping. The jQuery UI framework provides a sortable() function that helps in re-ordering list items by using the mouse. With this functionality, the list items beco
    3 min read
  • How to Add Edit and Delete Table Row in jQuery ?
    In this article, we will create an example of how to add, edit and delete table rows in jQuery. For creating this functionality we need to know about the basics of the HTML table, jQuery basic functionality. Table row: An HTML table is a combination of rows and columns. The table row is in the horiz
    3 min read
  • How to add table row in a table using jQuery?
    In jQuery, adding a row to a table refers to dynamically inserting a new table row (<tr>) into an existing HTML table. This functionality allows developers to update and manage table content in real-time, enhancing interactivity and user experience without reloading the page. Steps to add tabl
    2 min read
  • How to create To-Do List using jQuery?
    This article focuses on developing a To-do List with some basic features like: Add Task Delete Task Cross the completed tasks Prerequisites: Basic knowledge of Front-end development using HTML, CSS, JS, jQuery & Bootstrap-3. Steps: Initialize the layout: - Add a input text-box with a button to a
    4 min read
  • How to fetch data from JSON file and display in HTML table using jQuery ?
    The task is to fetch data from the given JSON file and convert data into an HTML table. Approach: We have a JSON file containing data in the form of an array of objects. In our code, we are using jQuery to complete our task. The jQuery code uses getJSON() method to fetch the data from the file's loc
    3 min read
  • How to Create an HTML Table from an Object Array Using JavaScript ?
    Tables are a fundamental part of web development, and displaying data in a structured manner is a common requirement. JavaScript provides a powerful way to dynamically generate HTML content, making it easy to create tables from object arrays. Table of Content Using innerHTML propertyUsing appendChil
    2 min read
  • Drag and Drop Sortable List Using HTML CSS & JavaScript
    The Drag and Drop Sortable List Project in JavaScript allows users to easily reorder items in a list using drag-and-drop functionality. This interactive tool enhances user experience by enabling smooth, dynamic list organization, ideal for applications like task managers or shopping lists. What we a
    5 min read
  • How to convert JSON data to a html table using JavaScript/jQuery ?
    To convert JSON data into an HTML table, there are multiple approaches, each of which has its own strengths. Let's walk through both approaches you mentioned, detailing how each one works. Table of Content Using for loopUsing JSON.stringify() MethodApproach 1: Using for loopTake the JSON Object in a
    4 min read
  • How to Drag and Drop Images using HTML5 ?
    In this article, we will see how to create a drag and drop functionality using HTML5. Approach: The following approach will be implemented to Drag and Drop Images using HTML5. We have given a rectangle area and an image, the task is to drag the image and drop it into the rectangle.We have to enable
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences