The id of the input seems is not WallSearch. Maybe you're confusing that name and id. They are two different properties. name is used to define the name by which the value is posted, while id is the unique identification of the element inside the DOM.
Other possibility is that you have two elements with the same id. The browser will pick any of these (probably the last, maybe the first) and return an element that doesn't support the value property.
The id of the input seems is not WallSearch. Maybe you're confusing that name and id. They are two different properties. name is used to define the name by which the value is posted, while id is the unique identification of the element inside the DOM.
Other possibility is that you have two elements with the same id. The browser will pick any of these (probably the last, maybe the first) and return an element that doesn't support the value property.
perhaps, you can first determine if the DOM does really exists,
function walkmydog() {
//when the user starts entering
var dom = document.getElementById('WallSearch');
if(dom == null){
alert('sorry, WallSearch DOM cannot be found');
return false;
}
if(dom.value.length == 0){
alert("nothing");
}
}
if (document.addEventListener){
document.addEventListener("DOMContentLoaded", walkmydog, false);
}
Uncaught TypeError: Cannot read properties of undefined (reading 'length') — DataTables forums
jquery - DataTables: Cannot read property 'length' of undefined - Stack Overflow
typeerror - JavaScript - Cannot read properties of undefined (reading 'length') - (Code does work and provided) - Stack Overflow
JS error Uncaught TypeError reading length - Support - Themeco Forum
It's even simpler: just use dataSrc:'' option in the ajax defintion so dataTable knows to expect an array instead of an object:
$('#pos-table2').DataTable({
processing: true,
serverSide: true,
ajax:{url:"pos.json",dataSrc:""}
}
);
See ajax options
CAUSE
This errors TypeError: Cannot read property 'length' of undefined usually means that jQuery DataTables cannot find the data in the response to the Ajax request.
By default jQuery DataTables expects the data to be in one of the formats shown below. Error occurs because data is returned in the format other than default.
Array of arrays
{
"data": [
[
"Tiger Nixon",
"System Architect",
"$320,800",
"2011/04/25",
"Edinburgh",
"5421"
]
]
}
Array of objects
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
}
]
}
SOLUTION
Use default format or use ajax.dataSrc option to define data property containing table data in Ajax response (data by default).
See Data array location for more information.
LINKS
See jQuery DataTables: Common JavaScript console errors for more details.
You should validate your data before performing operations on it:
UPDATED
$.ajax({
url: 'http://www.domain.pro/api/get_post/?post_id=' + postid + '&post_type=news',
async: false,
callback: 'callback',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'jsonp',
timeout: 5000,
success: function (data, status) {
if(data != undefined && data.post != undefined){
$('#news').append('<div>' + data.post.title + '</div><div>' + data.post.content + '</div><hr/>');
}
},
error: function () {
output.html('<h1 class="error">There was an error loading the data.</h2>');
}
});
you applying $.each on data.post which could be undefined if your data is null. $.each expects and array or collection that is why you getting an error for length. since your data is null hence data.post would be undefined. you could check before applying $.each like
if(typeof data == "object" && data.post)
//Your code here.
this check should be applied whenever you expect some data from ajax.