Search code examples
javascriptjqueryjquery-uijquery-ui-droppable

create unique IDs for selects in drop zone


I realize this question is similar to others posted, but I was hoping someone could help me to approach this problem:

I have a select element within a droppable item using JQuery drag-drop which is cloned when dragged. The element has an ID select but I need the ID to be unique so I can access the different selected options per box in the drop zone.

enter image description here

Below the drag and drop zone you see the select boxes IDs, my desired output is select_1, select_2, select_3 etc etc whenever new blocks are dragged

Right now, there is one original select box on the left with the id "select" and I want to dynamically create IDs for the select boxes in the drop zone.

I was wondering what the best way would be to give each select box within the drop zone it's own unique ID?

That way I can eventually extract the selected values (TWO, THREE, TWO, THREE as in photo above)

Desired Output

select_1     TWO
select_2     THREE
select_3     TWO
select_4     THREE

$(document).ready(function() {
  var dragOpts = {
      helper: 'clone',
      zIndex: 10
    },
    dropOpts = {
      tolerance: 'fit',
      drop: function(e, ui) {
        if (ui.draggable.hasClass('div-source')) {
          var cloneDiv = ui.draggable.clone(),
            cloneDragOpts = {
              containment: 'parent'
            };
          cloneDiv.css({
            position: 'absolute',
            top: ui.offset.top - $(this).offset().top,
            left: ui.offset.left - $(this).offset().left
          }).draggable(cloneDragOpts);
          $(this).append(cloneDiv);
        }
      }
    };

  $('#source div').each(function(index) {
    $(this).draggable(dragOpts);
  });

  $('#target').droppable(dropOpts);
});

/*
Experimenting with printing the new select IDs
Once dragged into the drop zone
*/
function drop(event) {
  $("#target select").attr("id", (index, oldId) => oldId + "_" + (index + 1))
  var IDs = $("#target select[id]")
    .map(function() {
      return this.id;
    })
    .get();
  document.getElementById("inside_drop_zone").innerHTML = IDs.join(", ")
}
#source {
  width: 150px;
  height: 100px;
  overflow: auto;
  float: left;
  border: 2px solid #696969;
  background: #d3d3d3;
}

#target {
  width: 500px;
  height: 100px;
  border: 2px solid #000;
  margin-left: 10px;
  float: left;
  border: 2px solid #696969;
  background: #d3d3d3;
  position: relative;
  z-index: 1;
}

#source div,
#target div {
  display: block;
  padding: 10px;
  margin: 20px;
  font-weight: bold;
  font-family: monospace;
  background-color: white;
  text-align: center;
  max-width: 70px;
  border: 5px solid black;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>


</script>
<div id="source">
  <div id="div1" class="div-source">
    <select id="selection">
      <option>ONE</option>
      <option>TWO</option>
      <option>THREE</option>
    </select>
  </div>
</div>

<div id="target" ondrop="drop(event)">
</div>

<div id="inside_drop_zone"></div>

I realize this post has overlap with others, but I was curious if I'm even going about this the best way and if there are any better suggestions to creating dynamic select boxes.

Thank you so much!


Solution

  • If you really need them to have unique ID's, ditch the default ID's entirely and add them by doing something like this:

    let sels = $('#target').children('select');
    for (let x = 0; x < sels.length; x++) {
      $(sels[x]).attr('id', 'select_' + x);
    }
    
    

    However those ID's won't necessarily be consistent if you rerun this code.