最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get id of a dropped element - jquery UI - Stack Overflow

programmeradmin0浏览0评论

I have tried many ways to get the id of dropped element in jquery UI. please help to get the value of id.

$( function() {    

    $(".draggable").draggable({
        revert: "invalid",      
        helper: "clone"     
    });

$( "#droppable2" ).droppable({
        drop: function( event, ui ) {
            var draggable = ui.draggable;           
            var dragged = draggable.clone(); 
            var currentID = ui.draggable.attr("id");/*draggable.find('id'); - returns an object. but, could not get the id. */

            alert(dragged.html());          
            alert(currentID);
            dragged.resizable();
            dragged.appendTo("#droppable2");

            //alert("open properties");
      }
    });

  } );

html of the dropped element returns and it contains the id.

---html---

<div class="ui-widget-content draggable">
                <select id='singleSelect'>
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="opel">Opel</option>
                  <option value="audi">Audi</option>
                </select>
            </div>

<div id='droppable2' class="ui-widget-header" height='100%'><p/></div>

I have tried many ways to get the id of dropped element in jquery UI. please help to get the value of id.

$( function() {    

    $(".draggable").draggable({
        revert: "invalid",      
        helper: "clone"     
    });

$( "#droppable2" ).droppable({
        drop: function( event, ui ) {
            var draggable = ui.draggable;           
            var dragged = draggable.clone(); 
            var currentID = ui.draggable.attr("id");/*draggable.find('id'); - returns an object. but, could not get the id. */

            alert(dragged.html());          
            alert(currentID);
            dragged.resizable();
            dragged.appendTo("#droppable2");

            //alert("open properties");
      }
    });

  } );

html of the dropped element returns and it contains the id.

---html---

<div class="ui-widget-content draggable">
                <select id='singleSelect'>
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="opel">Opel</option>
                  <option value="audi">Audi</option>
                </select>
            </div>

<div id='droppable2' class="ui-widget-header" height='100%'><p/></div>
Share Improve this question asked Feb 24, 2017 at 10:55 SujithSujith 1612 silver badges14 bronze badges 1
  • 1 Your markup is weird...what is this <p/>? ... in <div id='droppable2' class="ui-widget-header" height='100%'><p/></div> – Ionut Necula Commented Feb 24, 2017 at 10:59
Add a ment  | 

2 Answers 2

Reset to default 6

You need to find the select first and then get its id, because ui.draggable returns a nodeList, also as I said in my ment, you are using a <p> tag wrong, corrected that a bit:

$(function() {

  $(".draggable").draggable({
    revert: "invalid",
    helper: "clone"
  });

  $("#droppable2").droppable({
    drop: function(event, ui) {
      var draggable = ui.draggable;
      var dragged = draggable.clone();
      var currentID = ui.draggable.find('select').attr('id');
      console.log(currentID);
      dragged.resizable();
      dragged.appendTo("#droppable2");

      //alert("open properties");
    }
  });

});
<link href="https://cdnjs.cloudflare./ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery./ui/1.12.1/jquery-ui.min.js"></script>

<div class="ui-widget-content draggable">
  <select id='singleSelect'>
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="opel">Opel</option>
                  <option value="audi">Audi</option>
                </select>
</div>

<div id='droppable2' class="ui-widget-header" height='100%'>
  <p>some paragraph</p>
</div>

I would like to share that you can simply use this code to get id/class name of a dropped element:

ui.draggable.attr("yourid/yourclass");

add this code inside your droppable function.

发布评论

评论列表(0)

  1. 暂无评论