7

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

4
  • 2
    Seems to be working to me: jsfiddle.net/Rr2hf , try posting your jQuery version or include more code. Commented Mar 13, 2012 at 8:06
  • It works fine for me, which jQuery version are you using? jsfiddle.net/fCrb5 Commented Mar 13, 2012 at 8:07
  • also, check to see if a debugger(ie firebug) shows errors Commented Mar 13, 2012 at 8:09
  • Just on one browser or on all browsers? Commented Mar 13, 2012 at 8:58

3 Answers 3

6

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" );
} );
Sign up to request clarification or add additional context in comments.

2 Comments

Great! You save my time! happy coding
Cheers. I would like to add that in 2018 it might be bad for the user experience to request a double-click (A lot of your users might want to use a touchscreen where there is no double-click).
4

try use inside $(document).ready()

$(document).ready(function(){

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

});

Comments

1

When I try the code, it works just fine:

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

Check if there is something different from your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.