Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'ownerDocument' of undefined

I'm teaching myself AJAX to AJAXify my site. In my template, I have the following JS code to get some JSON data from a view then append the data to a div.

function filter(type) {   $.getJSON(     '/activity_stream/global-activity-stream/',      {xhr: "true", filter: type},      function(data) {       $('.mainContent').children().remove();       $(data).appendTo('.mainContent');     });   }    $(".btn").click(function () {      filter("recent");    }); } 

I think my view is returning proper JSON but now data is not being added to the .mainContent div.

It gives this error:

Uncaught TypeError: Cannot read property 'ownerDocument' of undefined.

like image 700
Denny Avatar asked Jul 11 '13 13:07

Denny


People also ask

What is cannot read property of undefined in JavaScript?

The TypeError: Cannot read property of undefined is one of the most common type errors in JavaScript. It occurs when a property is read or a function is called on an undefined variable. TypeError: Cannot read properties of undefined (reading x) Undefined means that a variable has been declared but has not been assigned a value.

What is the meaning of undefined in JavaScript?

Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.

Why can’t I call a function on an undefined variable?

In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.

Why does [0] return undefined when I put a Div?

The div element does not have a 0 property, so [0] returns undefined. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers.


1 Answers

Make sure you're passing a selector to jQuery, not some form of data:

$( '.my-selector' ) 

not:

$( [ 'my-data' ] ) 
like image 58
redolent Avatar answered Oct 05 '22 22:10

redolent