Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click function in jQuery is not working

I have two span elements in a page. when I call a jquery double click function on both then the function is called only on first element. I am using the following code:

<span id="shiftTime_1">1</span>
<span id="shiftTime_2">1</span>

and jquery function is:

 $("[id^='shiftTime_']").dblclick(function() {
 alert("hello");
 });

when I double click on the element Id of shiftTime_1. then the function works fine. But when I double click on element Id of shiftTime_2 then this function does not respond.

Please help. Thanks

like image 822
manishjangir Avatar asked Mar 13 '12 08:03

manishjangir


People also ask

How to trigger double click event in jQuery?

The dblclick() is an inbuilt method in jQuery which is used to trigger the double-click event to occur. This method occurs when the selected element will be double clicked. Syntax: $(selector).

How to solve double click problem in JavaScript?

The dblclick event generates an event on double click the element. The event fires when an element is clicked twice in a very short span of time. We can also use the JavaScript's addEventListener() method to fire the double click event. In HTML, we can use the ondblclick attribute to create a double click event.

What is double click function?

Double-clicking allows two different actions to be associated with the same mouse button. It was developed by Bill Atkinson of Apple Computer (now Apple Inc.) for their Lisa project. Often, single-clicking selects (or highlights) an object, while a double-click executes the function associated with that object.


2 Answers

Use .on if you plan to add elements dynamically (e.g. by using $( "body" ).append( "<span id='shiftTime_2'>1</span>" ); )

$( "body" ).on( "dblclick", "span", function() {
    alert( "This works also with dynamically added elements" );
} );
like image 84
Igor L. Avatar answered Sep 27 '22 21:09

Igor L.


try use inside $(document).ready()

$(document).ready(function(){

 $("[id^='shiftTime_']").dblclick(function() {
 alert("hello");
 });

});
like image 42
Kanishka Panamaldeniya Avatar answered Sep 27 '22 20:09

Kanishka Panamaldeniya