I am not sure why the event StopPropagation technique is not working with this simple onClick example - the event seems to be bubbling up the DOM tree and is also iterating exponentially every time I click again.
I think this is because I generate html with a javascript function, and I'd be very eager to hear why you think this isn't working as expected.
EDIT: I have created a jsFiddle at http://jsfiddle.net/Guill84/GsLtN/22/. The issue becomes very clear. If I click on 'Text1' the first time, the alert pop-up comes up only once. If I click it again, it then comes up twice. If I click it again, it comes up three times, and so on. How can I amend the script so that it is run only once? This is driving me barmy.
HTML
<body>
<div id='container'>
<div id='1'>Text1</div>
<div id='2'>Text2</div>
</div>
</body>
Javascript
$(function notice_it(declim) {
if ($.isNumeric(declim)) {
//stuff
} else {
declim = 10;
}
$("#container").html("<div id='1'>Text1</div><div id='2'>Text2</div>");
alert(declim);
$('body').on('click', '#1', function (event) {
event.stopPropagation();
var declim = 10 - 1;
notice_it(declim);
});
$('body').on('click', '#2', function (event) {
event.stopPropagation();
var declim = 10 + 1;
notice_it(declim);
});
$('body').on('click', 'body', function (e) {
e.stopPropagation();
});
});