JavaScript
x
5
1
var drag = document.getElementById("drag");
2
3
drag.onclick = function(e){
4
e.preventDefault();
5
}
JavaScript
1
3
1
#drag {
2
width: 50px;
3
}
JavaScript
1
5
1
<select id="drag" multiple="">
2
<option value="1">1</option>
3
<option value="2">2</option>
4
<option value="3">3</option>
5
</select>
is it possible with javascript to stop the hold-select effect and make it a drag to change elements order in the list?
i tried to e.preventDefault()
and to bind a draggable script
in options but did not work
Advertisement
Answer
I don’t think you can do that with select element :
Trying to change the code you provide making a simple one
index.html
JavaScript
1
17
17
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>title</title>
6
<link rel="stylesheet" href="style.css">
7
<script src="script.js"></script>
8
</head>
9
<body>
10
<ul>
11
<li>1</li>
12
<li>2</li>
13
<li>3</li>
14
</ul>
15
</body>
16
</html>
17
script.js :
JavaScript
1
13
13
1
$("ul").on('click', 'li', function (e) {
2
if (e.ctrlKey || e.metaKey) {
3
$(this).toggleClass("selected");
4
} else {
5
$(this).addClass("selected").siblings().removeClass('selected');
6
}
7
}).sortable({
8
connectWith: "ul",
9
delay: 150,
10
revert: 0,
11
12
});
13
However you can use multiselect plugin ,a demo here
EDITED :
If you still want the select tag look you can use dragOptions
JavaScript
1
10
10
1
$(document).ready(function(){
2
$("#drag").dragOptions({
3
onDrag: function(){
4
console.log('onDrag callback: ', this);
5
},
6
onChange: function(){
7
console.log('onChange callback: ', this);
8
}
9
});
10
});
JavaScript
1
19
19
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>drag</title>
6
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
7
</head>
8
<body>
9
<div class="form-group">
10
<select id="drag" size="5" class="form-control">
11
<option value="1">1</option>
12
<option value="2">2</option>
13
<option value="3">3</option>
14
</select>
15
</div>
16
</body>
17
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
18
<script src="https://gitcdn.link/repo/NikitaKA/jquery.dragOptions/master/jquery.dragoptions.min.js" type="text/javascript" language="javascript"></script>
19
</html>
As you see when you drag an option an error occurs (maybe an issue))
If someone can tell me what’s going wrong here i will appreciate that !
Hope that helps someone .