Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery, Uncaught TypeError

I have some javascript code on my webpage that is loading some divs onto the page. I also want to add onmouseenter, and onmouseleave event handlers to each div. I am using jquery to add these handlers, but i get the error:

"Property '$' of object [object DOMWindow] is not a function"

My code looks like this, it is in a for loop:

var newItem = document.createElement('div');
newItem.innerHTML = results[i];
newItem.setAttribute("id", "resultDiv_" + i.toString());
dropDown.appendChild(newItem);

//Error on next line...
$("resultDiv_" + i.toString()).bind("mouseenter", function() {
    $("resultDiv_" + i.toString()).css({ 'background-color': 'blue' });
});

Does anyone have any ideas why i am getting this error, or even what the error means?

like image 638
Pavol Avatar asked Oct 22 '09 13:10

Pavol


People also ask

How do you fix uncaught TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How do I fix uncaught ReferenceError jQuery is not defined?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

What is uncaught TypeError?

According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.

Is not a function error jQuery?

on if not a function" jQuery error occurs for 2 main reasons: Loading an old version of the jQuery library that doesn't support the on function. Loading a library that overrides the value of the dollar sign $ variable.


1 Answers

Try replacing all occurrences of $ with jQuery.

Also the selector $("resultDiv_" + i.toString()) won't likely return any elements. You probably meant: $("#resultDiv_" + i.toString())

And finally make sure this code is executed when the DOM is ready i.e. inside:

jQuery(function() {
    // Put your code here
});
like image 99
Darin Dimitrov Avatar answered Oct 02 '22 06:10

Darin Dimitrov