2

Let's say I have got HTML code that looks as follows:

<script>
  $("#draggable").draggable();

  $("#clickable").click(function() {
    alert('CLICK!');
  });
</script>

<div id="draggable">
    <div id="clickable">        
    </div>
</div>

As you can see: clickable div is nested in draggable div. When I drag & drop draggable div using clickable div as handle click event bound to it is fired. That is not desired behaviour. I want the click event to be fired only when you click on clickable but without dragging it anywhere. Is it even possible? I want to add I'm not interested in solutions where setTimeout (or similar) is used. Here you have got jsfiddle link (note: css is added for better visibility).

Thanks for any clues!

2 Answers 2

3

Here you go:

function handleClick() {
    alert('click!');
};

$("#draggable").draggable({
    start: function () {
        $('#clickable').off('click');
    },
    stop: function () {
        setTimeout(function(){
            $('#clickable').on('click', handleClick);
        },1);
    }
});

$("#clickable").on('click', handleClick);

Demo : http://jsfiddle.net/HmK9z/

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

3 Comments

well, I said that setTimeout is not good solution for me ;) but thank you nevertheless
It's required. You can always ask the UI team to implement a native workaround if you don't want to use it. github.com/jquery/jquery-ui
Probably I will need to use it. I thought there is better workaround to do this.Thanks again
2

Use the callback of the draggable method like this:

HTML:

<div id="draggable">
    <div id="clickable">        
    </div>
</div>

JQuery:

var dragged = false;
$("#draggable").draggable({stop:function(ev,ui) { dragged = true;}});

$("#clickable").click(function() {
    if(!dragged)
        alert('CLICK!');
    dragged = false;
});

JSFiddle: http://jsfiddle.net/AJmTg/9/

Good luck!

3 Comments

it's not exactly what I want. I don't want click event to be fired at all (of course when clickable is dragged). Nevertheless, thanks :)
If you don't want the click event to be fired, then why do you bind it to the clickable div?
to be fired when clicked but not dragged ;)

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.