Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:13:09+00:00 2026-05-23T07:13:09+00:00

I am using jQuery droppable (in conjunction with jQuery draggable ) to allow the

  • 0

I am using jQuery droppable (in conjunction with jQuery draggable) to allow the user to add rows to an HTML table by dragging items from a list and dropping them on the table.

This works well, however at present the logic is that when the user drag-drops on a table row the new row gets added below the row they dropped on.

It would be better if the new row’s add position was based on whether the user dropped in the upper or lower half of an existing row.

This is easy enough to calculate in the drop event, but I need to give UI feedback as the user drags (which I would do by means of two CSS classes droppable-above and droppable-below for example).

This doesn’t seem to be possible, as the over event only fires once; when the user initially drags over the droppable element.

Is it possible to get the over event to fire for every mouse move while the user is over a droppable element?

If so, then I’d be able to do this:

$("tr.droppable").droppable({
    over: function(event, ui) {
        if (/* mouse is in top half of row */) {
            $(this).addClass("droppable-above").removeClass("droppable-below");
        }
        else {
            $(this).removeClass("droppable-above").addClass("droppable-below");
        }
    },

    out: function(event, ui) {
        $(this).removeClass("droppable-above").removeClass("droppable-below");
    },

    drop: function(event, ui) {
        $(this).removeClass("droppable-above").removeClass("droppable-below");
        if (/* mouse is in top half of row */) {
            // Add new row above the dropped row
        }
        else {
            // Add new row below the dropped row
        }
    }
});

The CSS styles would be something like…

droppable-above { border-top: solid 3px Blue; }
droppable-below { border-bottom: solid 3px Blue; }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:13:10+00:00Added an answer on May 23, 2026 at 7:13 am

    As you said, over (like its counterpart out) is only raised once on the droppable. On the other hand, the drag event of the draggable is raised every time the mouse moves, and seems appropriate for the task. There are, however, two problems with this strategy:

    • drag is raised whether or not the draggable actually lies over a droppable,
    • even in that case, the droppable is not passed to the event handler.

    One way to solve both problems is to associate the droppable and the draggable in the over handler, using jQuery’s data() facility, and disassociate them in the out and drop handlers:

    $("tr.droppable").droppable({
        over: function(event, ui) {
            if (/* mouse is in top half of row */) {
                $(this).removeClass("droppable-below")
                       .addClass("droppable-above");
            }
            else {
                $(this).removeClass("droppable-above")
                       .addClass("droppable-below");
            }
            ui.draggable.data("current-droppable", $(this));  // Associate.
        },
    
        out: function(event, ui) {
            ui.draggable.removeData("current-droppable");     // Break association.
            $(this).removeClass("droppable-above droppable-below");
        },
    
        drop: function(event, ui) {
            ui.draggable.removeData("current-droppable");     // Break association.
            $(this).removeClass("droppable-above droppable-below");
            if (/* mouse is in top half of row */) {
                // Add new row above the dropped row.
            }
            else {
                // Add new row below the dropped row.
            }
        }
    });
    

    Now that the draggable knows the droppable it’s lying over, we can update the element’s appearance in a drag event handler:

    $(".draggable").draggable({
        drag: function(event, ui) {
            var $droppable = $(this).data("current-droppable");
            if ($droppable) {
                if (/* mouse is in top half of row */) {
                    $droppable.removeClass("droppable-below")
                              .addClass("droppable-above");
                } else {
                    $droppable.removeClass("droppable-above")
                              .addClass("droppable-below");
                }
            }
        }
    });
    

    The code that follows is a simple test case that demonstrates this solution (it basically fills the commented gaps above and refactors common patterns into helper functions). The droppable setup is a little more intricate than in the previous example, mainly because the newly created table rows have to be made droppable like their siblings.

    You can see the results in this fiddle.

    HTML:

    <div class="draggable">New item 1</div>
    <div class="draggable">New item 2</div>
    <div class="draggable">New item 3</div>
    <div class="draggable">New item 4</div>
    <div class="draggable">New item 5</div>
    <p>Drag the items above into the table below.</p>
    <table>
        <tr class="droppable"><td>Item 1</td></tr>
        <tr class="droppable"><td>Item 2</td></tr>
        <tr class="droppable"><td>Item 3</td></tr>
        <tr class="droppable"><td>Item 4</td></tr>
        <tr class="droppable"><td>Item 5</td></tr>
    </table>
    

    CSS:

    p {
        line-height: 32px;
    }
    
    table {
        width: 100%;
    }
    
    .draggable {
        background-color: #d0ffff;
        border: 1px solid black;
        cursor: pointer;
        padding: 6px;
    }
    
    .droppable {
        background-color: #ffffd0;
        border: 1px solid black;
    }
    
    .droppable td {
        padding: 10px;
    }
    
    .droppable-above {
        border-top: 3px solid blue;
    }
    
    .droppable-below {
        border-bottom: 3px solid blue;
    }
    

    Javascript:

    $(document).ready(function() {
        $(".draggable").draggable({
            helper: "clone",
            drag: function(event, ui) {
                var $droppable = $(this).data("current-droppable");
                if ($droppable) {
                    updateHighlight(ui, $droppable);
                }
            }
        });
    
        initDroppable($(".droppable"));
    
        function initDroppable($elements)
        {
            $elements.droppable({
                over: function(event, ui) {
                    var $this = $(this);
                    updateHighlight(ui, $this);
                    ui.draggable.data("current-droppable", $this);
                },
                out: function(event, ui) {
                    cleanupHighlight(ui, $(this));
                },
                drop: function(event, ui) {
                    var $this = $(this);
                    cleanupHighlight(ui, $this);
                    var $new = $this.clone().children("td:first")
                                    .html(ui.draggable.html()).end();
                    if (isInUpperHalf(ui, $this)) {
                        $new.insertBefore(this);
                    } else {
                        $new.insertAfter(this);
                    }
                    initDroppable($new);
                }
            });
        }
    
        function isInUpperHalf(ui, $droppable)
        {
            var $draggable = ui.draggable || ui.helper;
            return (ui.offset.top + $draggable.outerHeight() / 2
                    <= $droppable.offset().top + $droppable.outerHeight() / 2);
        }
    
        function updateHighlight(ui, $droppable)
        {
            if (isInUpperHalf(ui, $droppable)) {
                $droppable.removeClass("droppable-below")
                          .addClass("droppable-above");
            } else {
                $droppable.removeClass("droppable-above")
                          .addClass("droppable-below");
            }
        }
    
        function cleanupHighlight(ui, $droppable)
        {
            ui.draggable.removeData("current-droppable");
            $droppable.removeClass("droppable-above droppable-below");
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery UI's draggable and droppable libraries in a simple ASP.NET proof of
Using jQuery, how do I delete all rows in a table except the first?
Using jQuery, how do you bind a click event to a table cell (below,
Using jQuery Ajax to fetch data from local server: it works well with IE8
im using jQuery and im trying to create a menu using a list of
Using jQuery , how can I dynamically set the size attribute of a select
Using jQuery, how do you check if there is an option selected in a
Using jquery how do I focus the first element (edit field, text area, dropdown
Using jQuery, what's the best way to find the next form element on the
Using jQuery, is there a way to select all tag that reference a specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.