18

I can easily make a <div> element draggable, but not a <button> element. How can I make the <button> draggable, too?

$(init);

function init() {
  $('#makeMeDraggable1').draggable();
  $('#makeMeDraggable2').draggable();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"></script>

<div id="makeMeDraggable1">Drag me!</div>
<button id="makeMeDraggable1">Drag me!</button>

View at JSFiddle

0

6 Answers 6

58

Its not a bug , use this, $('input[type=button]').draggable({cancel:false}); You need to cancel the default click event of the button.

Sign up to request clarification or add additional context in comments.

1 Comment

this solved my problem too.So this is really the corrrect answer.
9

I haven't tested this myself, but I've got an intuition that it will have something to do with a button's default event-handler for mousedown. You might want to experiment with event.preventDefault() inside a mousedown handler.

Alternatively, you could wrap the button in a div that you then draggable().

1 Comment

You can't drag the button, only the div: if you click slightly to the right of the button, you should see that. What this suggests is that the event handler on the button is definitely interfering, so you probably need to cancel that.
5

JQuery disables drags from starting on the following elements by default:

  • input, textarea, button, select, option

See the current spec here: https://api.jqueryui.com/draggable/#option-cancel

This is (to me) unnecessarily opinionated and annoying. But luckily it's easy to disable. Simply adjust the cancel option. For example, the following draggable initialization allows drags to start on all elements except .no-drag classes:

$ele.draggable({
    cancel : '.no-drag'
});

Comments

1

looks like it's a jquery ui bug.

with another draggable plugin it works:

http://jsfiddle.net/CkKgD/

I found the plugin here: http://devongovett.wordpress.com/2009/12/01/event-delegated-drag-and-drop-jquery-plugin/

and in the first place I used it because of the lack of delegation support in jquery-ui draggable.

Comments

0

You can make a div look like button using CSS. That's what I did most of time in that kind of cases.

2 Comments

You might be able to get this working, but note that it's not ideal since it's much less semantic and you'll have to do custom event-handling for it, etc. Much better to fix the actual problem
@Bobby: True! But I'm still interested in seeing Taesung's approach live.
0

Having cancel:false following code would cancel the default click event:

$( "#btn1 " ).draggable({
   appendTo: "body",
   helper: "clone",
  cancel:false
});

Html part

<input type="submit" id="btn1" value="button">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.