Set the style to be displayed as a block element

I am working on a Rails application that contains some <li> elements with the CSS property display: none;. I want these elements to only appear on the page when they are being dragged. However, there are some elements that do not have the display: none; style. How can I achieve this?

Below is the HTML code snippet :

<div class="col-xs-12 col-md-6 col-lg-3">
    <li id="<%=video_upload.id %>"  <% if video_upload.invisible == true %> <%= "style=display:none;" %> <%end%>   >
        <% if mobile? %>
            <a href="<%= showvideo_path(video_upload.id) %>">
                <div style="background-color: white">
                    <%= image_tag("/images/upload_images/"+"#{video_upload.imagename}", height: '130', width: '200') %>
                </div>
            </a>
        <% else %>
            <div style="background-color: white">
              <img src="<%= '/images/upload_images/'+"#{video_upload.imagename}" %>" class="img-responsive" style="cursor:pointer" onclick="popVideo('https://app.box.com/embed/s/<%= video_upload.link[/([^\/]+)$/] %>');" >
            </div>
        <% end %>

        <span style="float: left">
            <%= File.basename(video_upload.imagename, '.*') %>
        </span>

        <span style="float: right">
            <%= link_to '', edit_video_uploads_path(video_upload), {class: 'btn btn-primary  glyphicon glyphicon-pencil', :style => 'color: white'} %>

            <%= button_tag(:del_flag => 'button', :id => video_upload.id ,:class => 'btn btn-danger user-approve-btn glyphicon glyphicon-trash') do
                 ""
            end %>
        </span>
    </li>
</div>

I am utilizing this JavaScript library for drag and drop.

Answer №1

Drag and display your <li> items when dragging, then hide them once the drag is complete:

document.querySelectorAll('li').forEach(item => {
   // Show item on drag
   item.addEventListener('dragstart', event => {
       event.target.style.display = 'block';
   });

   // Hide item after drag
   item.addEventListener('dragleave', event => {
       event.target.style.display = 'none';
   });
});

This code utilizes HTML5 native drag and drop functionality as explained in this tutorial. Give it a try and let me know if it works for you.

UPDATE:

If you only want to select one specific element, add an id="myElement" to that element and access it like this:

let myItem = document.querySelector('#myElement');

myItem.addEventListener('dragstart', event => {
   event.target.style.display = 'block';
});
myItem.addEventListener('dragleave', event => {
   event.target.style.display = 'none';
});

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

Enhancing CSS with Additional Properties

As a web developer, I recently had the opportunity to explore the new LOGIN PAGE preview of GMAIL and was very impressed with the Sign In button's UI. After examining the Page's CSS, I discovered some interesting properties, such as: **backgroun ...

Set the background color of the container row to extend the full width, encompassing

I am working on a grid container and I want to change the background color of the second row within it. Check out my Fiddle This is the HTML markup: <div class="totalContainer totalContainer__space"> <div class="totalContainer__ ...

Is it possible to alter the HTML structure using JavaScript?

Looking to shift the positions of 2 divs using JavaScript DOM manipulation, all without altering the HTML. <div class="div1"> this should move downwards</div> <div class="div2"> while this should move upwards</div&g ...

What could be the reason behind the issue of my HTML draggable feature not functioning properly on touch

I've set up a draggable div with headers and p tags, but I'm encountering an issue when trying to run it on a touch screen. The div tag isn't draggable in this scenario. Can anyone suggest the best solution for making this page touch screen ...

Automatically refreshing controller functionality in CodeIgniter

Greetings everyone, I'm looking for a way to automatically refresh a controller function every 5 seconds. Currently, I am using header('Refresh: 10.2'); within the controller function like this: public function delete() { heade ...

Is there a way to animate without specifying a duration?

Exploring the capabilities of the Animated component in react-native, I came across the powerful Animated.timing(); function which operates within a specific duration defined as duration: 2000,. While duration is useful in certain scenarios, I found myself ...

Do I have to include the sizes attribute when using w-descriptors in srcset?

After reading several articles discussing the use of srcset for device-pixel-density adjustments and art direction, I have come to a few conclusions: The State of Responsive Images in 2015 by Paddi Macdonnell srcset Part 2: The W descriptor and sizes att ...

Steps for Removing Multiple CSS Styles from an Element's Style:

When my application generates inline styles, I sometimes need to remove the height, width, and max-width styles from certain elements. I have identified the element using this code: const elems = window.$('.myElements'); window.$.each(elems ...

What is the best way to include an arrow in a dropdown menu?

I've been working on restyling a Select box and I want to add a CSS arrow that rotates as the Select box expands. However, I'm struggling to align the arrow properly with the Select box. I've tried using code from the internet, but it hasn&a ...

Guide to generating interactive material-ui components within a react application?

I am facing a challenge in creating a dynamic mui grid element triggered by a button click in a React component. When attempting to create let newGrid = document.createElement('Grid'), it does not behave the same way as a regular div creation. D ...

Tips for displaying the sum of a grouped field in the balloonText using Amchart

I recently started working with Amcharts and I am seeking advice on what I may be doing incorrectly. Below is the snippet of my JavaScript code: var chartData1 = []; generateChartData(); function generateChartData() { var month = new Array( ...

Utilizing AngularJS Directive to trigger a designated function upon change in an input file

My current project involves creating a scavenger hunt editor with various types of questions stored in an array. Each question can be of a different type, all displayed using ng-repeat. To achieve this, I utilize a JavaScript object representing a Questio ...

Tips for implementing a JavaScript Material Design framework in ReScript code?

I am attempting to integrate the material-ui library into a Rescript/React application. The code snippet below demonstrates how to display a button: @module("@material-ui/core/Button") external button: string = "default" @react.compone ...

Tips on parsing JSON string in a servlet

I found a post that seems to have the solution to what I am attempting to accomplish, however, it is not working for me. Although the servlet posts successfully and I can see my json object in the watch window under the _parameters member variable of the s ...

CSS styles from Angular 2 components spilling outside of their boundaries

Check out the Plunker where I'm facing an issue. "If you comment out the styles in the ThirdComponent, you can see it affecting the parent and sibling components' styles." – adriancarriger (Special thanks to Adrian) Within my component, I am i ...

The functioning of JavaScript's ajax capabilities

Need some help with a coding issue I'm facing. Can anyone provide suggestions for improving my code? I've come across a situation where the table is not updating when using a certain piece of code. However, upon further inspection, I found that ...

An Undefined Session: Leveraging Connect-Redis with ExpressJS and Node.js

Upon waking up this morning, I was greeted with an error message from Nodejitsu: Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process. Determined to find ...

Using the Map Function in React JS to Dynamically Render Radio Buttons with Material-UI

Hey everyone, I'm looking for some assistance in replacing the old classic radio button with a new one using material-ui. I've been trying but haven't been successful so far. Any suggestions would be greatly appreciated. Thanks in advance. Y ...

Transferring an object from one inventory to another

I'm in the process of developing a task manager that enables users to add and remove tasks. I am also working on enabling the ability for users to transfer tasks from one list to another. The current code I have written doesn't seem to be functio ...

In the event that you encounter various version formats during your work

Suppose I have a number in the format Example "1.0.0.0". If I want to increase it to the next version, it would be "1.0.0.1" By using the following regex code snippet, we can achieve this perfect result of incrementing the version to "1.0.0.1": let ver ...