i'm using the following
jquery 1.11.1
jquery.mobile 1.4.3
cordova 2.9.0
iscroll 5
here's my html:
<div data-role="page" id="listpageid">
<div data-role="header" data-position="fixed">
some radio buttons
</div>
<div id="wrapper">
<div data-role="content" id="scroller">
</div>
</div>
</div>
i have an ajax call that gets a bunch of data from a server. i loop thru the results and add them to the div with id="scroller"
$('#scroller').empty();
for (var i=0;i<resultsArr.length;i++)
{
htmlToAppend = "";
htmlToAppend = "<a id=" + i + " >" + resultsArr[i].field + "<br><br></a>";
contentForDisplay = contentForDisplay.concat(htmlToAppend);
}
$('#scroller').append(contentForDisplay);
$('#scroller a').click(function() {
alert("you clicked on " + this.id);
});
this code runs whenever an ajax call fills resultsArr
the first time the page displays and this is executed it gives me exactly what i want, a scrollable list of clickable links.
if i leave the page and navigate back however, things don't work properly.
the div named scroller is emptied and the contents of resultsArr are displayed but clicking on every link results in 2 alerts instead of 1.
if i leave the page and navigate back a 3rd time, clicking on a link gives 3 alerts instead of 1.
i have tried preventing this code
$('#scroller a').click(function() {
alert("you clicked on " + this.id);
});
from executing on all but the first page display and that makes the displayed links unclickable on the 2nd, 3rd etc page displays.
i thought
$('#scroller').empty();
removed/destroyed the contents of the page and would include the function that gets fired on the click event.
obviously its not.
how do i allow for page redisplay BUT only attach the function to the click event once?
EDIT: none of your solutions worked. until i removed iscroll. then ALL your solutions worked. so .... does anyone have any ideas about how iscroll is screwing this up?