Adjust the CSS of the currently dragged item using jQuery sortable

I am currently using jQuery-ui sortable and facing an issue with changing the appearance of a dragged item. I would like the text value of the component inside the red background container to be displayed while dragging. Can someone please assist me with this?

Here is my progress so far: http://jsfiddle.net/Fyd48/5/. It is almost working as expected, where dragging 'super dooper' displays it with a red background. However, there is still a gray box surrounding it that looks odd. I'm not sure where this gray box is coming from. Could someone help me remove it?

$( "#container" ).sortable({
start: function(event, ui){        
   var text = $.trim(ui.item.text());
   ui.item.startHtml = ui.item.html();

   ui.item.html('<div style="display: inline-block;" class="rf-ind-drag default drag">' + text + '</div>');
},
stop: function(event, ui){ 
   ui.item.html(ui.item.startHtml);
}
});
$( "#container" ).disableSelection();

Answer №1

Each of your draggable divs is styled with

background-color: rgb(245, 245, 245);
, causing the gray box to appear when dragged. Try removing this property for a different effect.

Check out the live demo here: http://jsfiddle.net/Fyd48/10/

Answer №2

The gray background is a result of the following DIVs:

<div style="border: 0px; background-color: rgb(245, 245, 245); padding: 0px; position: relative; background-position: initial initial; background-repeat: initial initial;" class="default ui-draggable">

When you drag and create the red DIV, it appears smaller than the containing div, allowing the parent background color to show through. To prevent this, try setting the height and width of the red DIV to 100%:

ui.item.html('<div style="display: inline-block; width:100%; height: 100%" class="rf-ind-drag default drag">' + text + '</div>');

(It's recommended to place the styling attributes in the style tag instead of using inline styling)

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

unable to alter attribute using jQuery

I'm struggling to update an attribute of an element using jQuery. Here is the HTML code: <input type="checkbox" name="row1" value="" > This is the corresponding jQuery code: $(document).ready(function (event) { $("input[type='checkb ...

Tips on designing checkboxes with CSS

Can someone help me figure out how to style a checkbox using the code below? <input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" /> I've tried applying this style but it doesn't seem to work. The chec ...

The JQuery ajax request is returning a 400 error code indicating a bad request

I've encountered a frustrating issue while using this JS code to send an ajax request: var data = JSON.stringify({ 'user_id': '<?=$user->id?>', 'package_id': '<?=$bundle->packa ...

What is the best way to display the user login in the center of a page with all necessary controls

Challenge How can the user login be displayed in the center of the page? Additional Information The current layout displays user login data on the left-hand side. I am looking to have the user login data centralized on the page, which includes labels f ...

Employing Modernizer.js to automatically redirect users to a compatible page if drag and drop functionality is not supported

I recently set up modernizer.js to check if a page supports drag and drop functionality. Initially, I had it set up so that one div would display if drag and drop was supported, and another div would show if it wasn't. However, I ran into issues with ...

Having trouble converting the JQuery result from the REST request into the correct format

Currently, I am working on making a REST request for an array of objects using JQuery. During the execution of the code within the "success" section, everything works perfectly fine - the objects in the array are converted to the correct type. However, I ...

Restoring styles back to the default CSS settings

I am currently working on creating visualizations using D3, and one challenge that I have encountered is the need to define a lot of styles within my code instead of in my CSS where I would prefer them to be. This necessity arises mainly to support transi ...

Combining input elements and captchas in one line within Bootstrap

I am currently working on a form group that includes a label, a captcha image, and an input field for the captcha code. My goal is to have the label positioned at the top of the form group, with the captcha image and input field aligned in one line. I have ...

Utilizing React to customize JVectorMap markers

Having an issue with a marker appearing in my React project https://i.stack.imgur.com/hkqnZ.jpg The markers are displaying correctly, but there is a persistent initial marker at 0,0 that I can't remove. Removing the 'markers' property from ...

Header Stability TransitionAndView

I'm not very experienced with JavaScript, so please bear with me. I'm attempting to create a fixed header that transitions to 50% opacity when scrolled down and returns to full opacity (opacity: 1.0) when scrolled back to the top. Here is my cur ...

What are some ways to implement querySelectorAll in conjunction with htmx?

I'm currently using htmx, an amazing library with a small issue that I'm struggling to resolve. htmx utilizes querySelector to find elements for swapping or updating, for example with hx-swap="...", hx-target="...". How can I use querySelectorAll ...

How can I toggle the visibility of a div based on whether a variable is defined or not?

How can I display a specific div on my webpage only when certain variables in PHP pull out a specific result from a database? I attempted to use the code snippet below, but it's not working as expected. Can someone provide guidance on how to achieve ...

Retrieving HTML Source Code in Android Studio

I am having trouble with testing this function even though the permissions are correctly set in the manifest file. Can anyone help me figure out what I'm doing wrong? public void fetchHTMLContent() throws Exception { URL url = new URL("http:/ ...

Ways to incorporate an image into an SVG file

This SVG image that I have created presents a unique design. <svg width="250px" height="250px" viewBox="0 0 250 250" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <def> </def ...

Resetting the value of a radio button input option to null using Angular2 ngModel

In my Angular2 application, I am attempting to implement radio button inputs using ngModel and value. The goal is to have three options: true, false, and null. However, I am struggling to assign a value of null to one of the inputs. Ideally, when nothing ...

Delete entries in table based on user-provided criteria

Hello there, I'm new to this community and seeking some assistance. I am currently learning jQuery/Javascript and have encountered a problem that has me stumped. The issue arises with a table where rows are generated based on a user-selected number f ...

Delete the current row in the table before adding a new one with jquery

Having a HTML table and performing updates using Ajax. Everything is functioning properly so far. When clicking on the Edit button, the values are displayed in the textboxes correctly. However, when clicking on the Add button, the table is updated accordin ...

HTML stubbornly resists incorporating Javascript [UIWebView]

Currently, I am facing an issue while trying to animate a color property using jQuery 1.7.1 and the jquery color plugin downloaded from this link. I have ensured that all necessary files are included and part of the build target. Here is a simplified versi ...

Cascading Style Sheets variable and Sassy CSS mixin

I'm facing a challenge involving the use of CSS variables in conjunction with my VueJs app. The goal is to make a hover effect (changing background-color) customizable by the application, while having a default value set in a nested SCSS map using the ...

jQuery/JSON Error: Unexpected character encountered during parsing

I am currently working on MVC4 and I am attempting to transfer data from a view to a controller using JQuery and JSON. The objective is to extract the values of checkboxes within a grid. Here is the code snippet: <script type="text/javascript"> func ...