Momci, navukli ste me na jQuery, sad pomagajte...
Postavio sam sledeću temu na jQuery mailing listu, rekoh možda bi i neko od vas hteo da porazmisli... Tema je na engleskom ali je čini mi se sve jasno.
Hi everybody,
I would be very grateful if someone could explain to me behaviour of the following simple piece of code, which is from my perspective a bit strange:
PHP kôd:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#btn1').click(
function() {
alert('btn1 clicked');
}
);
$('#btn3').click(
function() {
alert('btn3 clicked');
$('#btn1').click();
$('#btn1').get(0).onclick();
$('#btn1').get(0).click();
$('#btn2').click();
$('#btn2').get(0).onclick();
$('#btn2').get(0).click();
}
);
});
function btn2Click() {
alert('btn2 clicked');
}
</script>
</head>
<body>
<input type="button" id="btn1" value="btn1" />
<input type="button" id="btn2" value="btn2" onclick="btn2Click()" />
<input type="button" id="btn3" value="btn3" />
</body>
</html>
The same page could be found at:
http://radioni.ca/jquery-click.html
so you can try it out yourselves.
Click on the third button causes following alerts to be displayed:
- btn3 clicked
- btn1 clicked
- btn1 clicked
- btn2 clicked
- btn2 clicked
- btn2 clicked
- btn2 clicked
Why btn1 only twice, and btn2 even four times? (Note: is behaviour from Firefox, seems like IE behaves slightly different. Let's stay with FF for some time)
Thanks in advance, I am not asking this just for fun, I have problems with submit event and I have created this code to simplify and illustrate.