Presuming you have divs with IDs divtext_0
... divtext_4
for(var i=0;i<5;i++) {
$("#divtext_"+i).click(function(e) {
onClicked();
});
}
function onClicked() {
alert("Clicked")
}
At the moment you are adding multiple click handlers to the div divtext_100
- rather than adding one click handler to multiple divs.
It is hard to say what is best without seeing your HTML - but if you gave all your divs a class, e.g. 'clickable', then you can avoid any loops and make any div with that class clickable.
<div class="clickable"></dvi>
You could simply do.
$(".clickable").click(function(e) {
onClicked();
});
function onClicked() {
alert("Clicked")
}