0

I have a div tag in my html. I want to disable it after I double click on it.

<div id="mydiv">
</div>


    $('#mydiv').on('click', function (event) {

        var count = parseInt($('#counter').html(), 10)
        count += 1
        $('#counter').html(count)
        $('#counter').attr("disabled", disabled)

    })

3 Answers3

1

You can use the dblclick event as follows - this works exactly as the click event but the counter is no longer necessary

HTML

<div id="myDiv" style="background:red">Has not been double clicked</div>

JS (JQUERY)

$('#myDiv').on("dblclick",function(){
  this.innerHTML = "Was double clicked";
});

Live example - https://playcode.io/374412?tabs=script.js,preview

0

Jquery provides dblclick and unbind function. dblclick function trigger when mydiv is double clicked by user. You can write your code in instead alert. In addition if you want to remove the dblclick event once it is used or clicked

$( "#mydiv" ).dblclick(function() {
alert("sample");
//your code will go here
  $(this).unbind();
});
vaibhav kulkarni
  • 1,733
  • 14
  • 20
-1
    let clicked = 0;
    $('#mydiv').on('click', function (event) {
    clicked =clicked+1;
    if(clicked>=2){
            $('#counter').attr("disabled", disabled); 
    } 
   })
SHUBHAM SINGH
  • 371
  • 3
  • 11
  • 1
    A double click = two clicks in low interval of time, your solution will add the `disabled`attr if user click two times in 5 min without reloading the page – Mickaël Leger Jul 15 '19 at 08:43