Magesh
Magesh

Reputation: 308

Jquery double click event is not working

My requirement is to just double click a button.

HTML:

 <button id="btn-db" class="dbBtn" ></button>

Solutions tried:

  $("#btn-id").trigger("dblclick");

is not triggering double click event

Note: I've tried the same thing for single click,

  $("#btn-id").trigger("click"); 

Single click is triggering by invoking above statement in browsers console.

Upvotes: 0

Views: 2362

Answers (2)

Takit Isy
Takit Isy

Reputation: 10081

This is working:

$("#btn-id").on('dblclick', function() {
  console.clear();
  console.log("You double-clicked!");
});

$("#btn-id").trigger("dblclick");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn-id" class="dbBtn">Double click</button>

I just noticed this is a typo error:
<button id="btn-db" class="dbBtn" ></button>: id should be btn-id in your code!

Hope it helps.

Upvotes: 0

Sumit Jha
Sumit Jha

Reputation: 1691

Try button.on("dblclick", ...). Here is an example.

var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("dblclick", function(){
  banner.addClass("alt")
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner-message">
  <p>Hello World</p>
  <button>Change color</button>
</div>

Upvotes: 1

Related Questions