appending not working and not showing error too - javascript

I am trying to call function.to open the popup to the screen.but it not show the pop up and not showing error too.function is calling properly tried print detail using console.log. Data is coming properly.i think some issue while appending the popup division.please help me out.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function viewCamp(id) {
$('#customModal').remove();
listable = "<h1 style='text-align: center;'>Camp Details<h1>";
$.get("camp-details-ajax.php?e2c=" + id, function(data) {
obj = $.parseJSON(data);
console.log(obj);
listable += "<table style='text-align:left'>";
listable += "<tr><th>Organization name</th><td>" + obj.CAMP_SCHOOL_NAME + "</td></tr>";
listable += "<tr><th>Name of contact person</th><td>" + obj.CAMP_SCHOOL_PRINCIPAL_NAME + "</td></tr>";
listable += "<tr><th>Mobile Number</th><td>" + obj.CAMP_SCHOOL_CONTACT_NUM + "</td></tr>";
if (obj.CAMP_SCHOOL_LANDLINE != '' && obj.CAMP_SCHOOL_LANDLINE != null) {
listable += "<tr><th>Landline Number</th><td>" + obj.CAMP_SCHOOL_STD_CODE + "-" + obj.CAMP_SCHOOL_LANDLINE + "</td></tr>";
}
listable += "<tr><th>Email</th><td>" + obj.CAMP_SCHOOL_EMAIL + "</td></tr>";
listable += "<tr><th>Address1</th><td>" + obj.CAMP_SCHOOL_ADD1 + "</td></tr>";
listable += "<tr><th>Address2</th><td>" + obj.CAMP_SCHOOL_ADD2 + "</td></tr>";
if (obj.CAMP_SCHOOL_ADD3 != '' && obj.CAMP_SCHOOL_ADD3 != null) {
listable += "<tr><th>Address3</th><td>" + obj.CAMP_SCHOOL_ADD3 + "</td></tr>";
}
listable += "<tr><th>City </th><td>" + obj.CAMP_SCHOOL_CITY + "</td></tr>";
listable += "<tr><th>State</th><td>" + obj.CAMP_SCHOOL_STATE + "</td></tr>";
listable += "<tr><th>Pincode</th><td>" + obj.CAMP_SCHOOL_PIN + "</td></tr>";
listable += "<tr><th>Doctor Name</th><td>" + obj.CAMP_TEMP_DENT_NAME + "</td></tr>";
listable += "<tr><th>Doctor Email</th><td>" + obj.CAMP_TEMP_DENT_EMAIL + "</td></tr>";
listable += "<tr><th>Doctor Mobile</th><td>" + obj.CAMP_TEMP_DENT_MOBILE + "</td></tr>";
listable += "<tr><th>Strength of Whole School/Organization</th><td>" + obj.CAMP_NO_OF_STUDENT + "</td></tr>";
listable += "<tr><th>Number of Students/Children attending</th><td>" + obj.CAMP_NO_OF_STU_ATTEND + "</td></tr>";
listable += "</table>";
$('body').append(' <div class="modal fade" id="customModal" role="dialog"><div class="modal-dialog"> <div class="modal-content"><div class="modal-body"><button type="button" class="close" data-dismiss="modal"><img src="images/close.png" class="img-responsive" width="100%" ></button><p>' + listable + '</p></div></div></div></div>');
});
}
$('body').on('click', '.close', function() {
$('#customModal').remove();
})
</script>

Related

How can I get text value from modal with javascript?

I'm fetching the values from my table in ajax.
$(data.mesIzinTanimList).each(function (index, element) {
if (element.izinTanimiVarmi == 'yok')
tr = "<tr class='bg-warning text-warning-50' row='" + element.userId + "' >";
else
tr = "<tr class='bg-light text-white-50 ' row='" + element.userId + "' >";
tr += "<td>" + element.sicilNo + "</td>";
tr += "<td>" + element.adSoyad + "</td>";
tr += "<td>" + element.bagliOlduguKisi + "</td>";
tr += "<td>" + element.bagliOlduguKisiSicilNo + "</td>";
tr += "<td style='display:none'>" + element.bagliOlduguKisiId + "</td>";
tr += "<td> <button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")' type='button' class ='btn btn-info'> Tanımla </button></td>";
tr += "</tr>";
$('#IzinTanimListe').append(tr);
});
I open a modal with the define Btn onclick feature.
function tanimlaBtn(userId, adSoyad, bagliOlduguKisiId, bagliOlduguKisi) {
document.getElementById('userId').value = userId;
document.getElementById('bagliOlduguKisiId').value = bagliOlduguKisiId;
document.getElementById('bagliOlduguKisi').value = bagliOlduguKisi;
document.getElementById('adSoyad').value = adSoyad;
}
The adSoyad and bagliOlduguKisi information appears as undefined. They should be string values.
How can I solve this?
The parameters for the function should be wrapped in doubles quotes and escaped e.g \"
Try replacing:
<button onclick= 'tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ," + element.bagliOlduguKisiId + ", " + element.bagliOlduguKisi + ")'
With:
<button onclick='tanimlaBtn(" + element.userId + " , " + element.adiSoyadi + " ,\"" + element.bagliOlduguKisiId + "\", \"" + element.bagliOlduguKisi + "\")'

Generate table with images on javascript

I want to create a button that produces a 3x3 HTML table meant for a card game, where each cell corresponds to one single image. For the time being, I'm mainly focusing on filling individual cells with a card number taken from an array element, using a step-by-step rationale. This is what the final result should look like in HTML, derived from the produced script;
<table style="width:100%">
<tr>
<td><img src="1.png"></td>
<td><img src="2.png"></td>
<td><img src="3.png"></td>
</tr>
<tr>
<td><img src="4.png"></td>
<td><img src="5.png"></td>
<td><img src="6.png"></td>
</tr>
<tr>
<td><img src="7.png"></td>
<td><img src="8.png"></td>
<td><img src="9.png"></td>
</tr>
</table>
This is what I'm currently working at, although with not much success
<!DOCTYPE html>
<html>
<body>
<table id="1">
</table>
<button onclick="createTable()">Create</button>
<script>
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" +
;
}
</script>
</body>
</html>
Note
The 9-card deck is shuffled using a pretty basic and self-explanatory function.
Edit. My final version, which is returning broken image.
<!DOCTYPE html>
<html>
<body>
<table id="1">
</table>
<button onclick="createTable()">Create</button>
<script>
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) {
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
document.getElementById("1").innerHTML = "<tr><td><img src=" + deck[0] + ".png'></td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>"
;
}
</script>
</body>
</html>
After you fix your code by removing the extra + at the last line where you build your html (tr's and td's), you will simply need just to replace
"<td>" + deck[0] + "</td>"
by
"<td><img src='" + deck[0] + ".png'></td>"
... and so on for other indexes like deck[1], deck[2] etc.
You have got an extra "+"
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>";
Should work better.
Your shuffle() is pretty convoluted. Let me advise you to write less exotic code. Making your code more readable will help to find potential errors. Indeed, the problem is not so hard to locate actually: https://stackoverflow.com/a/34471591/1636522 :-)
var deck = [1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write(JSON.stringify(shuffle(deck), 0, 1));
function shuffle (anArray) {
var i, j, x, l = anArray.length;
for (i = 0; i < l; i++) {
j = rdmInt(l);
x = anArray[i];
anArray[i] = anArray[j];
anArray[j] = x;
}
return anArray;
}
function rdmInt (max) {
return Math.floor(Math.random() * max);
}
The same remark applies to your string concatenation:
document.getElementById("1").innerHTML = ""
+ "<tr>"
+ "<td><img src=" + deck[0] + ".png'></td>"
+ "<td>" + deck[1] + "</td>"
+ "<td>" + deck[2] + "</td>"
+ "</tr>"
+ "<tr>"
+ "<td>" + deck[3] + "</td>"
+ "<td>" + deck[4] + "</td>"
+ "<td>" + deck[5] + "</td>"
+ "</tr>"
+ "<tr>"
+ "<td>" + deck[6] + "</td>"
+ "<td>" + deck[7] + "</td>"
+ "<td>" + deck[8] + "</td>"
+ "</tr>";
Can you see the error now (line 3)? Here is a fix:
+ "<td><img src=\"" + deck[0] + ".png\"></td>"
You could go even further to avoid repetitions (dry):
var deck = [
"KNhxd", "7CtbR", "Os8qX",
"21SKd", "CWMZC", "43C1X",
"lpGvK", "8Wk7W", "Y3JFi"
];
// preserve the global namespace with an IIFE
document.body.innerHTML = function () {
var ROOT = "http://i.stack.imgur.com/";
function toTable (deck) {
var i, html = "";
for (i = 0; i < 3; i++) {
html += toTr(deck.slice(i * 3, (i + 1) * 3));
}
return "<table>" + html + "</table>";
}
function toTr (row) {
return "<tr>" + row.map(toTd).join('') + "</tr>";
}
function toTd (cell) {
return "<td><img src=\"" + ROOT + cell + ".png\" /></td>";
}
return toTable(deck);
}();
body{background:#006600;}
img{display:block;width:35px;}
If you want to create HTML elements, then you do not just write them as a text string and insert into an existing HTML element.
You create an element node (and text node if applicable) and append that to your existing HTML element.
Here is an article on w3 schools about creatung HTML tags with JavaScript.
http://www.w3schools.com/jsref/met_document_createelement.asp
If your table is always 9 cells then why not just write the HTML out giving each cell a unique id. Then u could use code similar to what u have.
document.getElementById("cell_1").innerHTML = deck[0];
You would need to include this logic for all 9 cells / values, although you could loop through them like;
for (i = 0; i < 8; i++) {
var cellNo = i + 1;
document.getElementById("cell_" + cellNo).innerHTML = deck[i];
}
Hope this helps ;)
In addition to the unnecessary '+' at the end of your innerHTML addition,
I would suggest making your code more flexible. Instead of adding the data hardcoded to your innerHTML you can do something like this:
var endOfRowCounter = 0;
var toAppend = "";
deck.forEach(function(card){
if(endOfRowCounter % 3 == 0) {
toAppend += "<tr>";
}
toAppend += "<td><img src=\"" + card + ".png\"></td>";
if(endOfRowCounter % 3 == 2)
{
toAppend += "</tr>";
}
endOfRowCounter++;
});
When I load your code I get this message in the browser console:
Uncaught SyntaxError: Unexpected token ;
It might help if you update this code:
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>" +
;
To this:
document.getElementById("1").innerHTML = "<tr><td>" + deck[0] + "</td><td>" + deck[1] + "</td><td>" + deck[2] + "</td></tr>" +
"<tr><td>" + deck[3] + "</td><td>" + deck[4] + "</td><td>" + deck[5] + "</td></tr>" +
"<tr><td>" + deck[6] + "</td><td>" + deck[7] + "</td><td>" + deck[8] + "</td></tr>";
To loop your array and create your table structure with the images, you could do this for example:
function createTable() {
var deck = [1,2,3,4,5,6,7,8,9];
function shuffle(o) { //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(deck);
var html = '';
for (var i = 0; i < deck.length; i++) {
if (i%3 === 0) {
html += "<tr>";
}
html += '<td><img src="' + deck[i] + '.png"></td>';
if (i%3 === 2) {
html += "</tr>";
}
}
document.getElementById("1").innerHTML = html;
}

how do i append in this function?

i have this code:
for (var i = 0; i < data.times.length; ++i) {
var time = formatTime(data.times[i].time);
tableContent += '<tr><td>' + data.times[i].destination.name + '</td><td id="appendLate' + i + '">' + time + '</td><td>' + data.times[i].track + '</td><td>' + data.times[i].train_type + '</td><td>' + data.times[i].company + '</td></tr>'
// laat vertragingen zien (BETA)
if (data.times[i].delay / 60 >= 1) {
$('#appendLate' + i + '').append("+" + data.times[i].delay / 60).addClass("late");
}
}
table.html(tableContent);
}
The if statement appends stuff and adds a class. i know it wont work like this.. but i cant seem to get how it WIL work.. Can some one help?
See it live: http://codepen.io/shiva112/pen/JGXoVJ?editors=001
Well, you're basically almost there. The right way to do it is to build the entire string before doing any DOM manipulation, since DOM operations are very slow (relatively).
Let's say your index.html looks like:
<html>
<head>
<title>Cool site!</title>
</head>
<body>
<table id="myCoolTable"></table>
</body>
</html>
Then your JavaScript simply becomes:
var tableContent = '';
for (var i = 0; i < data.times.length; ++i) {
var time = formatTime(data.times[i].time);
tableContent += '<tr>'
+ '<td>' + data.times[i].destination.name + '</td>';
if (data.times[i].delay / 60 >= 1) {
tableContent += '<td id=\'appendLate\'' + i + ' class=\'late\'>' + time + '</td>' + '+' + (data.times[i].delay / 60);
} else {
tableContent += '<td id=\'appendLate\'' + i + '>' + time + '</td>';
}
tableContent += '<td id=\'appendLate\'' + i + '>' + time + '</td>'
+ '<td>' + data.times[i].track + '</td>'
+ '<td>' + data.times[i].train_type + '</td>'
+ '<td>' + data.times[i].company + '</td>'
+ '</tr>';
}
$('#myCoolTable').html(tableContent);
What this does is build the HTML for the entire table. Then update the table only once. Hope it helps!

How to retrieve dynamically generated ids from input type?

Currently, I have an array of questions stored in a json file, using this code it will call the questions accordingly with the following script, which is then appended to a div called questions. How do I get the dynamically generated ids of the input types into the changeFont function so that not just the questions but also the inputs would be able to have a change in size
<script>
var array = JSON.parse(arrayQuestions);
console.log(array);
for (var i = 0; i < array.length; i++) {
var question = array[i];
var questionDiv = document.createElement("div");
var html = "<form>";
html = question.question + " <br> ";
var choices = question.choices;
for (j = 0; j < choices.length; j++) {
var choicesOpt = choices[j];
if (question.type == "radio") {
html += "<fieldset data-role=\"controlgroup\">";
html += "<input type=\"radio\" data-theme=\"b\" name=\"" + i + "\" id=\"" + i + choicesOpt + "\" value=\"" + choicesOpt + "\" /><label for=\"" + i + choicesOpt + "\">" + choicesOpt + "</label>";
html += "</fieldset>";
} else if (question.type == "checkbox") {
html += "<fieldset data-role=\"controlgroup\">";
html += "<input class=\"banana\" type=\"checkbox\" data-theme=\"b\" name=\"" + i + "\" id=\"" + i + choicesOpt + "\" value=\"" + choicesOpt + "\" /><label for=\"" + i + choicesOpt + "\">" + choicesOpt + "</label>";
html += "</fieldset>";
} else if (question.type == "textbox") {
html += "<input class=\"banana\" type=\"text\" data-theme=\"b\" data-clear-btn=\"true\" name=\"" + i + "\" id=\"" + i + choicesOpt + "\" value=\"" + choicesOpt + "\">";
} else {
html += "<textarea name=\"" + i + "\" id=\"" + i + choicesOpt + "\"></textarea>";
}
html += "";
}
html += "</form>";
// html += "<input type=\"button\" onclick=\"myFunction()\" value=\"Submit form\"></form>";
questionDiv.innerHTML = html + "<br>";
document.getElementById('questions').appendChild(questionDiv);
}
//code to change font size
function changeFont(selectTag) {
var listValue = selectTag.options[selectTag.selectedIndex].text;
document.getElementById("#" + choicesOpt).style.fontSize = listValue;
}
$("#" + choicesOpt).each(function () {
changeFont(selectTag);
});
</script>

JavaScript error with XML when trying to read empty node

I am trying to render a catalog from an XML document and everything works, although when trying to show a default image if there is no image present in the XML node, it disables the parent node therefore forcing it not to show any of it and it reads me an error in my console saying:
Uncaught TypeError: Cannot read property 'getElementsByTagName' of
undefined
Which is pointing to this part of the code:
if (records[i].getElementsByTagName("IMAGE")[0].childNodes.length > 0)
{
So my question is where in my code below is my error causing it to not load the XML node?
PS: If anybody needs more of my code please let me know, and I would be happy to post it.
for (var i = fromItem; i < nextMaxItem; i++) {
if (records[i].getElementsByTagName("IMAGE")[0].childNodes.length > 0) {
xmlContent += '<article class="post all ' + records[i].getElementsByTagName("CATEGORY")[0].childNodes[0].nodeValue + '" id="">'
+ '<div class="col-sm-4 col-lg-4 col-md-4"><div class="thumbnail img-hover">'
+ '<a class="fancybox" rel="group" href="' + records[i].getElementsByTagName("BIGIMAGE")[0].childNodes[0].nodeValue + '" title="'+ records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue +'">'
+ '<img class="product-image" src="' + records[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + '" alt="' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '" />'
+ '<div class="caption">'
+ '<h4>' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '</h4>'
+ '<p>' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '</p>'
+ '</div>'
+ "</a></div></div></article>";
} else {
xmlContent += '<article class="post all ' + records[i].getElementsByTagName("CATEGORY")[0].childNodes[0].nodeValue + '" id="">'
+ '<div class="col-sm-4 col-lg-4 col-md-4"><div class="thumbnail img-hover">'
+ '<a class="fancybox" rel="group" href="' + records[i].getElementsByTagName("BIGIMAGE")[0].childNodes[0].nodeValue + '" title="'+ records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue +'">'
+ '<img class="product-image" src="/images/Products/no-preview.jpg" alt="No Preview Available" />'
+ '<div class="caption">'
+ '<h4>' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '</h4>'
+ '<p>' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '</p>'
+ '</div>'
+ "</a></div></div></article>";
}
}
Here is the whole JavaScript code:
var page = 1, perPage = 12, content = document.getElementById('content'),
pagination = document.getElementById('pagination'), records;
function paganation(page) {
var nextMaxItem = perPage * page,
fromItem = (page - 1) * perPage,
maxPages = Math.ceil(records.length / perPage);
var xmlContent = '<div class="row">';
for (var i = fromItem; i < nextMaxItem; i++) {
if (records[i].getElementsByTagName("IMAGE")[0].childNodes.length > 0) {
xmlContent += '<article class="post all ' + records[i].getElementsByTagName("CATEGORY")[0].childNodes[0].nodeValue + '" id="">'
+ '<div class="col-sm-4 col-lg-4 col-md-4"><div class="thumbnail img-hover">'
+ '<a class="fancybox" rel="group" href="' + records[i].getElementsByTagName("BIGIMAGE")[0].childNodes[0].nodeValue + '" title="' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '">'
+ '<img class="product-image" src="' + records[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue + '" alt="' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '" />'
+ '<div class="caption">'
+ '<h4>' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '</h4>'
+ '<p>' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '</p>'
+ '</div>'
+ "</a></div></div></article>";
} else {
xmlContent += '<article class="post all ' + records[i].getElementsByTagName("CATEGORY")[0].childNodes[0].nodeValue + '" id="">'
+ '<div class="col-sm-4 col-lg-4 col-md-4"><div class="thumbnail img-hover">'
+ '<a class="fancybox" rel="group" href="' + records[i].getElementsByTagName("BIGIMAGE")[0].childNodes[0].nodeValue + '" title="' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '">'
+ '<img class="product-image" src="/images/Products/no-preview.jpg" alt="No Preview Available" />'
+ '<div class="caption">'
+ '<h4>' + records[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + '</h4>'
+ '<p>' + records[i].getElementsByTagName("SHORTDESCRIPTION")[0].childNodes[0].nodeValue + '</p>'
+ '</div>'
+ "</a></div></div></article>";
}
}
xmlContent += "</div>";
content.innerHTML = xmlContent;
var paginationContent = '<nav><ul class="pagination">';
if (page > 1) {
paginationContent += '<li>';
paginationContent += '<span aria-hidden="true">«</span>';
paginationContent += '</li>';
} else {
paginationContent += '<li class="disabled">';
paginationContent += '<span aria-hidden="true">«</span>';
paginationContent += '</li>'
}
var prevPosition = page - 3;
var nextPosition = page + 3;
for (var i = 1; i <= maxPages; i++) {
if (i != page) {
if (i >= prevPosition && i <= nextPosition) {
var linkToPage = i == prevPosition ? 1 : i == nextPosition ? maxPages : i;
var linkText = i == prevPosition ? "..." : i == nextPosition ? "..." : i;
paginationContent += "<li>";
paginationContent += '' + linkText + '';
paginationContent += "</li>";
}
} else {
paginationContent += "<li class='active'>";
paginationContent += '' + i + '';
paginationContent += "</li>";
}
}
var next = page + 1;
if (next <= maxPages) {
paginationContent += '<li>';
paginationContent += '<span aria-hidden="true">»</span>';
paginationContent += '</li>';
} else {
paginationContent += '<li class="disabled">';
paginationContent += '<span aria-hidden="true">»</span>';
paginationContent += '</li>';
}
paginationContent += '</ul></nav>';
pagination.innerHTML = paginationContent;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", xmlUrl, false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
records = xmlDoc.getElementsByTagName(xmlNode);
records = Array.prototype.slice.call(records).sort(function () {
return Math.random() > 0.5 ? 1 : -1
});
paganation(1);
If we imagine that records was of length 18, then for the second page, you would have:
fromItem - 12
nextMaxItem - 24
You are iterating i from fromItem to nextMaxItem, but as soon as you go past i = 18, you've gone past the end of records. You need to make sure that doesn't happen. You need to ensure that you don't iterate past the end of your array:
var max = Math.min(nextMaxItem, records.length);
for (var i = fromItem; i < max; i++) {
....
}

Categories

Resources