Need Help - styling of AJAX created table not persisting
Hi guys, I'm new to JQuery and am having trouble with some css styling that I have applied to a table created via an AJAX request. I have a div off screen which scrolls into view when the appropriate link is selected. This div contains a table which is populated via an AJAX request to the database.
The success function of the AJAX request contains some styling which is applied when the call is made. The table is simply a list of football teams. Once the list is in view you can select one of the teams and further ajax requests are made to load data into the page. All fine up to this point.
However, once I've selected a team (and the div scrolls back off the screen) or if I undertake any other action on the page, then if I select the list again all the teams are still there but I've now lost the styling.
The only way I can get the styling back is to call the whole AJAX post again which just adds delay to the web page. My question is why does the styling from the inital load not persist?
The main AJAX call is as follows:
- $(function getTeams() {
- var sproc = "getTeamList";
- var competition = null;
- var params = new Object();
- params.sproc = sproc;
- params.competition = competition;
- $.ajax({
- type: "POST",
- url: "btsmethods.aspx/GetTeams",
- data: JSON.stringify(params),
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: teamlistSuccess,
- failure: function (response) { alert(response.d); },
- error: function (response) { alert(response.d); }
- });
- });
- function teamlistSuccess(response) {
- var xmlDoc = $.parseXML(response.d);
- var cstype = "";
- var xml = $(xmlDoc);
- var customers = xml.find("Table");
- var row = $("[id*=teams] tr:last-child").clone(true);
- $("[id*=teams] tr").not($("[id*=teams] tr:first-child")).remove();
- $.each(customers, function () {
- var customer = $(this);
- var image_src = $(this).find("Badge").text();
- var cstype;
- var sb = "images/mobile/"
- sb += image_src;
- sb += "mob.jpg";
- var bstr = $(this).find("Badge").text().substring(0, 2);
- $("td", row).eq(0).html('<img src="' + sb + '">');
- $("td", row).eq(1).html($(this).find("TeamID").text())
- $("td", row).eq(1).hide();
- $("td", row).eq(3).html($(this).find("Competition").text())
- $("td", row).eq(3).hide();
- $("td", row).eq(4).html($(this).find("CompetitionName").text())
- $("td", row).eq(4).hide();
- if (bstr == "sh") {
- $("td", row).eq(2).html($(this).find("TeamName").text());
- $(row).css("background-color", "#ffffff");
- }
- else {
- $("td", row).eq(2).html($(this).find("TeamName").text().substring(3, ($(this).find("TeamName").text().length)));
- $(row).css("background-color", "#96d986");
- }
- $("[id*=teams]").append(row);
- row = $("[id*=teams] tr:last-child").clone(true);
- });
- }
I then have code to add a click event to this as follows:
- $("[id*=teams]").on('click', 'tr', function () {
-
- var bsrc;
- var strb;
-
- document.getElementById("hdteamid").value = $(this).find('td:eq(1)').text();
- document.getElementById("hdteam1name").value = $(this).find('td:eq(2)').text();
- document.getElementById("hdleagueid").value = $(this).find('td:eq(3)').text();
- document.getElementById("hdleaguename").value = $(this).find('td:eq(4)').text();
- document.getElementById("t1").innerHTML = document.getElementById("hdteam1name").value;
- getteambadge();
-
-
- $("#hdteamid").trigger("change");
- });
The HTML for the table is as follows:
- <asp:Table ID="teams" Width="100%" CellPadding="0" CellSpacing="0" BorderStyle="None" CssClass="testtable" runat="server">
- <asp:TableRow CssClass="mobrow">
- <asp:TableCell ID="teambadge" Width="85px"></asp:TableCell>
- <asp:TableCell ID="TeamID" ></asp:TableCell>
- <asp:TableCell ID="TeamN"></asp:TableCell>
- <asp:TableCell ID="compid"></asp:TableCell>
- <asp:TableCell ID="compname"></asp:TableCell>
- </asp:TableRow>
- </asp:Table>
I am using a Master Page if this makes any difference.
Can anyone tell me why this behaviour is happening and how I might correct it?
Regards
Steven