Adarsh
Adarsh

Reputation: 46

function is not triggered while dragging DIV

I have .main-container DIV inside which there are 2 child DIV and I want to make these DIV's draggable.

I'm making use of jquery-ui for acheiving this. I'm able to drag if I write the below code :

index.js

$( function() {
    $( ".text1" ).draggable({containment: 'parent'});
  } )


  $( function() {
    $( ".text2" ).draggable({containment: 'parent'});
  } )

Now I want to make the above code dynamic i.e I want to create a function which will receive class name as a parameter.

Like this:

function drag(val){
    console.log("here", val)
        $(val).draggable({containment: 'parent'});

  }

However when I create a function it's not working. Someone let me know what is wrong over here.

index.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>New POC</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div class="main-container">
        <div class="text1" ondrag="drag('.test1')">Text 1</div>
        <div class="text2" ondrag="drag('.test2')">Text 2</div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script src="./index.js"></script>
</body>
</html>

Upvotes: 0

Views: 36

Answers (1)

Kei Huynh
Kei Huynh

Reputation: 49

You need to include an additional attribute which is draggable='true' to the tags:

<div class="text1" draggable='true' ondrag="drag('.test1')">Text 1</div>
<div class="text2" draggable='true' ondrag="drag('.test2')">Text 2</div>

Upvotes: 2

Related Questions