shibbir ahmed shibbir ahmed - 6 months ago 11
jQuery Question

jQuery dblclick() issue?

I have 2 input fields which is

a) email and

b) private_email

and I want to show alert message 1 when user double click on email field and 2 when user double click on private_email field. So that I am using following jQuery code but its showing only 1 in alert message :

jQuery Code :

$(document).ready(function(){
$("input#email, input#email_private").dblclick(function(){
var email = $("input#email").val();
var private_email = $("input#email_private").val();
var cdid = $("#cdid_hidden").val();
var cid=$('#cdid_hidden').val();

if(email) {
alert(1);
window.open('mailto:<?php echo $res['email']; ?>?subject=', '_self');
} else {
window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');
alert(2);

}

var url = "response.php?confirmEmail=confirm&cdid="+cdid;
$.get(url);
getDetails(cid);

});
});

Answer

Just check if the element is clicked is() one or the other, and execute accordingly.

$(document).ready(function(){
  $("input#email, input#email_private").dblclick(function(){  
    var email = $("input#email");
    var emailVal = email.val();
    var private_email = $("input#email_private").val();
    var cdid =  $("#cdid_hidden").val();    
    var cid=$('#cdid_hidden').val();

    if($(this).is(email)) {
        alert(1);
        window.open('mailto:<?php echo $res['emailVal']; ?>?subject=', '_self');   
    } else {
        window.open('mailto:<?php echo $res['email_private']; ?>?subject=', '_self');   
        alert(2);

    }       

    var url = "response.php?confirmEmail=confirm&cdid="+cdid;   
    $.get(url); 
    getDetails(cid);    

  });
});