Double click function in jQuery not working

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

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

      

and the jquery function:

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

      

when i double click on Id item shiftTime_1. then the function works fine. But when I double click on the Id item shiftTime_2, this function is unresponsive.

Please, help. Thanks to

+3


source to share


3 answers


try using inside $(document).ready()



$(document).ready(function(){

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

});

      

+3


source


When I try to execute the code, it works fine:

http://jsfiddle.net/Guffa/pfQfK/



Check if there is something different from your code.

+1


source


Use . on if you plan on adding elements dynamically (for example using $( "body" ).append( "<span id='shiftTime_2'>1</span>" );

)

$( "body" ).on( "dblclick", "span", function() {
    alert( "This works also with dynamically added elements" );
} );

      

+1


source







All Articles