Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this jQuery event run twice?

When you run this code and click input2, it will get the focus and .result div will output "f2" once. But when you click input1, the script will let input2 get the focus and .result div will output "f2" twice, why?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>blank</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){   
    $(".input1").click(function(){$(".input2").focus();});
    $(".input2").focus(function(){$(".result").html($(".result").html()+"f2, ");});
});
</script>
</head>

<body>
    <p>
        <input class="input1" value="click me to set the input2's focus" />
        <input class="input2" value="input2" />
        <div class="result"></div>
    </p>
</body>
</html>
like image 244
csdxf Avatar asked Feb 23 '23 01:02

csdxf


1 Answers

This is a known jquery bug :

http://bugs.jquery.com/ticket/6705

IE incorrectly calls the focus twice.

like image 58
Dominic Goulet Avatar answered Mar 03 '23 16:03

Dominic Goulet