Issues with JQuery draggable functionality not functioning as expected

Is there a way to drag bookmarks on a line and have the text above them change to show the percentage from the right edge? Here is a quick snippet that achieves this, along with a demonstration in this fiddle.

Check out the code below:

HTML

<div id='wrapper'>
   <div id="container"></div>
</div>

CSS

#wrapper {
  width:500px;
  height:100px;
  margin:40px 0px;
  border:1px solid white;
}

#container {
  background: green; 
  width: 500px; 
  height: 20px; 
  position: relative;
  margin-top:40px;
}
#wrapper img {
  position: absolute;   
}

span{font-size:62.5%;}

JQuery

$(document).ready(function() {
$("#container").click(function(e) {
    e.preventDefault();
    var x = e.pageX - this.offsetLeft;
    var y = e.pageY - this.offsetTop;
    var z = x - 5;       
    var txt = $('<span></span>');
    txt.css('top', '50px').css('left', z).css('position', 'absolute');
    txt.text( (Math.round((x.toFixed(0) / 5) * 1) / 1) + "%");
    txt.appendTo('#wrapper');

    var img = $('<img>');
    img.css('top', '65px').css('left', x).css('position', 'absolute').css('z-index', '999');
    img.attr('src', 'http://www.appmalt.info/htmlplay/img/arrowup.png');
    img.appendTo('#wrapper');

})

$('img').draggable();

});

I'm having trouble dragging the image in the fiddle. Can anyone help me figure it out?

Here's the fiddle link again.

.

Answer №1

Prior to inserting the image into the document, you have invoked the draggable function. Give this a try...

$(document).ready(function() {
    $("#container").click(function(e) {
        e.preventDefault();
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        var z = x - 5;       
        var txt = $('<span></span>');
        txt.css('top', '50px').css('left', z).css('position', 'absolute');
        txt.text( (Math.round((x.toFixed(0) / 5) * 1) / 1) + "%");
        txt.appendTo('#wrapper');

        var img = $('<img>');
        img.css('top', '65px').css('left', x).css('position', 'absolute').css('z-index', '999');
        img.attr('src', 'http://www.appmalt.info/htmlplay/img/arrowup.png');
        img.appendTo('#wrapper');
        $('img').draggable();
    })



});

Answer №2

$('img').draggable(); will only work on images that currently exist in the document.

If you want to make newly added images draggable, you will need to reapply the draggable function manually:

$('#container').on('click', function(e) {
  ...

  $(img).draggable();
});

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

Limited to IE6 and 7 - Issue with CSS/jQuery animation

After putting in 8 hours of non-stop work, I'm feeling pretty drained and need some assistance with fixing a bug that's specific to IE6/7. Any jQuery/CSS experts out there willing to lend a hand? To check out the issue, please visit this link us ...

Ensuring Email Authenticity in React Native

I'm currently working on email validation and have a specific condition in mind. If the email textbox is empty, I want to return true When the email textbox is not empty, I need to check if the input is a valid email address If the email is n ...

display the presently active dot in the slick slider

I am currently facing an issue with my slider. It currently shows the total slide count as the number of dots, but I also want to display the current active dot number instead of the active slide number. Here is the relevant section of my code: var $status ...

A fixed position header adjusts based on the content's offset

In the body of my web page, I have a fixed header and a div#content element. My original intention was to have the content pushed under the fixed header using the margin-top attribute. However, I noticed that when I applied this style, the header also move ...

"Error message: Undefined index error when trying to decode JSON

In my database, there's a JSON file named articles.json which contains data as follows: { "articles": [ { "id": 1, "title": "a" } ] } Now, I have written a script to decode this JSON file: <?php include "inc/header. ...

Generating JSON responses using Django REST framework to facilitate table visualizations on the front end

I am facing an issue with my django-rest-framework where I am trying to display a table using the Google Chart data drawing library. However, I keep encountering an error message saying "You called the draw() method with a String rather than a DataTable or ...

Having trouble aligning a div vertically inside another div?

I am struggling to vertically align a div within another div. I have experimented with vertical-align, position: relative; top: 50%, and margin: auto; without success. Take a look at the code below: .main { font-family: "adobe-garamond-pro"; pad ...

When making an AJAX request, ensure the response is in JSON format with individual properties rather than as a single

Here's an example of an AJAX request with JSON dataType: JavaScript: function checkForNewPosts() { var lastCustomerNewsID = <?php echo $customers_news[0]['id']; ?>; $.ajax({ url: "https://www.example.com/check_for_n ...

Vast expanse of emptiness within the table's header

I'm currently working on creating a table that includes both textual data and graphical representation. The issue I am facing is that the first header of the table contains a large empty space, as shown in the image below: https://i.sstatic.net/IeqER ...

How can the style of a div's :after pseudo-element be modified when hovering over its :before pseudo-element?

Here is some CSS code: .myDiv:before{ content:''; width:15px; height:15px; display:block; } .myDiv:after{ ... ... display:none; } And here is the corresponding HTML code: <div class='myDiv'></div> ...

Why is my request working on my local machine but not reaching the server?

My HTML website with JQuery is sending an Ajax POST request to a node server on my local Windows machine, and everything works perfectly. However, when I try to do the same on my VPS (droplet), the POST route doesn't seem to receive any data. Strangel ...

Tips for managing multiple events within a single component in Angular 4

Currently, I am working on developing a user interface layer for an application using Angular 4. The page layout I have consists of displaying data in tables based on search criteria. At the moment, I have code that displays the data in one HTML file (rec ...

Enhanced Organic Draping/Drifting

Currently working on creating a basic thumbnail gallery where clicking on a thumbnail expands it to one of three sizes (square, vertical, or horizontal). However, facing the issue where expanded pictures maintain their original order instead of filling emp ...

Unable to drag and drop functionality is not functioning properly in Android Emulator with Html and jQuery

As a newcomer to Android, I have successfully created a project in HTML that runs perfectly as intended. However, when trying to run this HTML project on an Android emulator, I encountered an issue with drag and drop functionality not working. I utilized ...

Removing Shadow, Border, and Outline from Bootstrap v5.0.2 Navbar

I have been struggling to get rid of the shadow/outline/border on my navigation bar. Despite trying various CSS tweaks, I have not been successful. Below is the specific HTML code: <nav class="navbar navbar-expand-sm" id="site-navigation ...

Having several contact forms embedded within a single webpage

I am currently in the process of developing a prototype website and my objective is to have multiple forms on a single page. My goal is to have a form for each service, where users can click on the service and fill out a form to request a quote. The first ...

Using jQuery to select an option with a button

Can a special <option> item be selected with jQuery by clicking a button? For example: <a id="item1" href="#">Item 1</a> <a id="item2" href="#">Item 2</a> <a id="item3" href="#">Item 3</a> <form> ....... & ...

Outlook's limited width is causing images to exceed the boundaries of the HTML email

I have a collection of images that display perfectly in Gmail, but when viewed in Outlook, all five images appear within a single <tr> and cause the table width to expand. My desired layout is to have the first four images centered on the first line ...

Uploading an image via Chrome and transferring it to a server using PHP: a step-by-step guide

In my web application, I am looking to allow users to easily upload images to a server. My idea is for the user to simply paste the image into their Chrome browser (e.g. a print screen), then have the image stream posted to a PHP page where it will be conv ...

What is the best way to keep the calendar of a Datepicker always visible while still being able to select a date easily?

When I write my code, the calendar only appears when I press the TextBox. I attempted to place the datepicker in a <div>, but then I was unable to retrieve the selected date. @model Plotting.Models.CalendarModel @using (Html.BeginForm("Calendar", "H ...