bdev bdev - 3 months ago 68
jQuery Question

knockoutjs single and double click

I want to be able to bind a single and double click event to a span of text. I know I can use

data-bind ="event: { dblclick: doSomething }


for a double click, but I also need the ability to perform a different function on single click. Any suggestions?

Answer

First, I wouldn't recommend click binding at all. Instead you should use "click" and "dblclick" handlers from jQuery:

$(someParentElement).on('click', 'your span selector', function (event) {
    var myViewModelFragment = ko.dataFor(this);
    // your code here
});

$(someParentElement).on('dblclick', 'your span selector', function (event) {
    var myViewModelFragment = ko.dataFor(this);
    // your code here
});

Edit: see also Niko's suggestion regarding supporting both single and double clicks. Basically, you should count the number of clicks manually and call different functions accordingly. I assumed jQuery handles this for you but, unfortunately, it doesn't.

Comments