Include draggable functionality to a seating chart that is generated dynamically

I developed a function that generates table seats dynamically with equal spacing. The issue arises when I try to drag names from a list and drop them onto the seats as children. Unfortunately, I can't seem to achieve this functionality with the dynamically created seat divs. Any suggestions on how to solve this?

For better readability, here is the JSBin link: http://jsbin.com/papoqih/edit?html,css,js,output

function createSeating(seatCount){
  
  var space = 1 / seatCount;
  var seatingHTML = '';
  
  for( i=0; i<seatCount; i++){
    var rotationAngle = (space * 360) * i;
  seatingHTML += '<div class="seat-wrap"\ style="transform:rotate('+rotationAngle+'deg);">\ <div class="seat"></div></div>\
              </div>'
  }

  document.querySelector('.seats').innerHTML = seatingHTML;

}
$("select[name=tableCap]").change(function() {
  createSeating($(this).val());
});

$(".nameContainer").draggable();
$(".seat").droppable({
  drop: function(ev, ui)
  {
    $(ui.draggable).appendTo($(this))
  }
});

$(function(){
  createSeating($("select[name=tableCap]").val());
});
html, body
{
  height: 100%;
}
#seatingChartContainer
{
  width: 80%;
  height: 100%;
  display: inline-block;
  position: relative;
  float:right;
  border: 1px solid black;
}
#seatingChartListContainer
{
  width: 20%;
  height: 100%;
  display: inline-block;
}
.seatingChartListContainer h3
{
  width: 20% !important;
}
.seatingChartTable
{
  border: 1px solid black;
  border-radius: 100%;
  position: absolute;
  top: 25%;
  left:50%;
  transform:translate(-50%, -50%);
  height: 200px;
  width: 200px
}
.seats
{
  height: 500px;
}
.seat
{
  width: 50px;
  height: 50px;
  border-radius: 100%;
  border: 1px solid black;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: all;
}
.seat-wrap {
  height: 50%;
  width: 100%;
  position: absolute;
  pointer-events: none;
  transform: translateY(-50%);
}
.nameContainer
{
  display: inline-block;
  width: 100%;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script src="https://raw.githubusercontent.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>
</head>
<body>
 
  <div id="seatingChartListContainer">
     Number of Seats
    <select type="select" id="tableCap" name="tableCap" />
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      </select>
    <p>Assigned to Table</p>
    <div class="nameContainer">
    <img src="http://www.iconsdb.com/icons/preview/black/contacts-xxl.png" width="50" height="50" style="display: inline"/><p style="display: inline">James</p>
    </div>
    <div class="nameContainer">
    <img src="http://www.iconsdb.com/icons/preview/black/contacts-xxl.png" width="50" height="50" style="display: inline"/><p style="display: inline">James</p>
    </div>
    <div class="nameContainer">
    <img src="http://www.iconsdb.com/icons/preview/black/contacts-xxl.png" width="50" height="50" style="display: inline"/><p style="display: inline">James</p>
    </div>
    <div class="nameContainer">
    <img src="http://www.iconsdb.com/icons/preview/black/contacts-xxl.png" width="50" height="50" style="display: inline"/><p style="display: inline">James</p>
    </div>
    <div class="nameContainer">
    <img src="http://www.iconsdb.com/icons/preview/black/contacts-xxl.png" width="50" height="50" style="display: inline"/><p style="display: inline">James</p>
    </div>
  </div>

  <div id="seatingChartContainer">
      <div class="seatingChartTableContainer">
  <div class="seatingChartTable"></div>
  <div class="seats"></div>
    </div>
  </div>
  </div>
</body>
</html>

Answer №1

It's not possible to import a raw version of a script file from Github because it won't be parsed as JS due to the different MIME type sent by the server.

To make your demo work, try changing the source for the file (jquery-ui-touch-punch):

http://jsbin.com/pucadabigo/edit?html,css,console,output

Edit: Also, make sure to use the correct selector .seats instead of .seat for the droppable element.

Edit 2: It turns out it wasn't a typo, but there were missing seat divs. I have added them back in along with some CSS fixes to clear the absolute positioning of the .nameContainer element after dragging.

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

In the realm where jQuery meets Django, a mysterious pair of brackets mysteriously appear in my JSON data, causing form validation to

In my Django model, there is a ManyToMany field named images, and I have created a simple ModelForm for this model. I am attempting to send data using jQuery along with a basic UpdateView. The field in the model images = models.ManyToManyField( RawI ...

Parsing a remote XML file using jQuery

I'm having some trouble with a jQuery script that I use to parse an XML file. It seems to only work when the XML file is located on my local server. Does anyone know how I can parse an XML file that is stored on a remote server? <script>$(docum ...

Create a new tab without triggering the pop-up blocker by utilizing an iframe's JavaScript event

I currently have an iframe embedded in a webpage. Once data is successfully sent to the server within the iframe, the server responds with a new URL to be opened either in a new tab or the parent window. The main issue I am encountering is that the brows ...

Discover the "route" of a JSON element using JavaScript

I've been struggling to retrieve the "path" of a specific AngularJS scope variable. My end goal is to utilize this "path" as the ng-model for dynamically generated forms. Below is my current code: my_code.js: var my_data = { name: "fred", numbe ...

Is it possible to transfer state to the getServerSideProps function in any way?

As a newcomer to next.js, I have a question about passing page state to getServerSideProps - is it achievable? const Discover = (props) => { const [page, setPage] = useState(1); const [discoverResults, setDiscoverResults] = useState(props.data. ...

Struggling to center an image within a CSS border

I'm attempting to include a subtle border around an icon. The issue I am facing is that the image is positioned at the top of the bordered area, whereas I want it centered. Here's how it currently appears: This is my current border CSS style: ...

Is utilizing v-model to update the Vuex store a recommended practice?

Hello there! As a newcomer to Vue, I find myself facing a dilemma that has been weighing on my mind. I'm confused about whether we should utilize the v-model directive to make changes to the Vuex store. While Vuex guidelines suggest modifying the stor ...

iOS experiences a lag in loading dynamic images

I have created a unique jquery carousel specifically designed for mobile devices. It features three containers that display three images at a time, with jquery handling the animation. By detecting the user's swipe direction (left or right), I update t ...

How can one decrease the size of a React project?

After running npx create-react-app my-app, the download was quick but then it took over an hour for the installation process to complete. Upon checking the size of the newly installed project, I found that the node_modules folder alone was a whopping 4.24 ...

Efficiently managing modules with requirejs and Backbone.Marionette

After organizing the file structure of my web app, utilizing RequireJs and Backbone.Marionette, it now looks like this: |- main.js |- app.js |- /subapp1 |- subapp1.js |- subapp1.router.js |- /subapp2 |- subapp2.js | ...

"Learn how to transfer a selected value from a parent ASP.NET dropdownlist to a textbox in a popup window using JavaScript

I'm struggling to pass the value from a dropdown list in the parent ASPX form to a textbox in the child ASPX form. Parent JavaScript: The first script is used to open the popup window <script type="text/javascript"> var popup; ...

Avoiding the Popover Component from Covering the Screen When the Menu Component Opens in React Material UI

I have implemented a Menu component to display a menu upon hovering over an element. However, I have encountered an issue where the menu includes a Popover component that opens up and covers the entire screen as an overlay, preventing interaction with th ...

Communication between Python and the front-end

I am currently working on a web application that utilizes jQuery for ajax requests to communicate with PHP, which in turn interacts with a MySQL database. I now have the need to implement some machine learning functionalities using Python, specifically for ...

Strategies for avoiding asynchronous behavior in AJAX with JQuery

My questions are best explained through code rather than words. Take a look at the snippet below and try to understand it: $('#all').on('change', function(){ //ONCHANGE RADIOBUTTON, THERE ARE #bdo, #cash also. $.ajax({ type:"GET", ...

Choose an element that has been generated dynamically using jQuery

Here is an example HTML table to work with: <table id="#myTable"> <tr id="#row123"><td>Content</td></tr> </table> To insert a new row using jQuery, you can use the following code: $('#myTable').prepend(&ap ...

"Mastering the art of event delegation: A guide to effectively engaging with various

I have data that is generated dynamically, as shown in the snippet below: const resultsDiv = document.getElementById("results"); const getList = document.getElementById("example"); document.querySelector(".container").addEventListener("click", function ...

Hide popup using HTML when clicking outside of it

Currently, I have implemented Bootstrap Popover with HTML content using the code snippet below. This code is based on the answer provided at . $(".song-status-link").popover({ html: true, placement: 'bottom', ...

Extracting a value from one HTML element and transferring it to another HTML element's attribute

Is there a way to retrieve the value of an "alt" attribute from an HTML <img> element and then assign it as the value for the "title" attribute in an enclosing <a> tag using PHP? <a title="" href ="page.html"><img src="image.jpg" al ...

Tips for aligning the navigation bar, main content, and footer in the center

Exploring the realm of web coding for the first time, I find myself in need of some guidance. My current project involves creating a webpage for the local sports team to replace their outdated one on Wordpress. Finding the platform not very user-friendly, ...

Is there a way to input data when a radio button is clicked using jquery, php, and mysql?

Following the generation of a list of data in a table using PHP and MySQL, which includes radio buttons, dropdown boxes, and text boxes for each row from the database, I am hoping to implement AJAX functionality to update the database on clicking a radio b ...