Amir Hesari Amir Hesari - 2 months ago 9
Javascript Question

Error after insert HTML code in DOM in console (Chrome browser)

Get HTML in line:

room["html"] = $(this).parents(".selectRowRoom").html();
and insert after load
step-02.php
in
.thisRoom
but this error is in Console.

$(function () {
var hotel = [];
var lenHotel = 0;
$('#forPrice').on('click', function() {
$('select').each(function() {
if($(this).val() > 0 && $(this).val() < 10) {
var room = {};
room["room"] = $(this).attr('room');
room["price"] = $(this).attr('price');
room["val"] = $(this).val();
room["html"] = $(this).parents(".selectRowRoom").html();

hotel.push(room);
}
});
lenHotel = Object.keys(hotel).length;
console.log(hotel);
});

$('#forPrice').click(function () {
$('#Step-01').hide();
$('#Step-02').show();
$('.hiddenAndShow').hide();
$( "#Step-02" ).load( 'step_02', function () {
for(var i = 0; i <= lenHotel; i++) {
$("#Step-02").find('.thisRoom').append("<tr class=\"selectRowRoom\">");
$("#Step-02").find('.thisRoom').append(hotel[i]['html']);
$("#Step-02").find('.thisRoom').append("</tr>")
}
});
});
})


And error in console is:

Uncaught TypeError: Cannot read property 'html' of undefined

Answer

This...

 for(var i = 0; i <= lenHotel; i++) 

Should be this

for(var i = 0; i < lenHotel; i++) 

Your accessing an element in the array that doesnt exist.