Keef Baker Keef Baker - 1 year ago 281
jQuery Question

hasClass() is not a function Jquery


I had character blindness with this one and it was missing a dollar. Despite looking at the code in detail and running it through a number of online lint programs I didn't pick up on the missing dollar.

It was just one of those things and I'm sorry.


I've looked on stack overflow and the similar problems I've found appear to be when calling against an array, but I'm calling against an Id.

When I run the following I get:

Uncaught TypeError: "#consoleLog".hasClass is not a function


if(^LCERROR/) ||^ERROR/)) {
if (!('#consoleLog').hasClass('stop')){
setInterval(function() {
$('#consoleLog').animate( { backgroundColor : "#aa0000" }, 1000)
.animate( { backgroundColor : "black" }, 1000);
}, 100);

With my limited understanding of Jquery and Javascript I thought an Id was a valid DOM to call hasClass() with. It even has examples of it here:

Any idea what might be happening here?

Purpose of the code is:
If the socket message contains ERROR or LCERROR, flash this div between black and red unless the stop class has been called.

Answer Source

You missed off the jQuery constructor function literal ($) in your if() statement:

if(^LCERROR/) ||^ERROR/) ) 
    if( ! $('#consoleLog').hasClass('stop') )
        setInterval(function() {
            $('#consoleLog').animate( { backgroundColor : "#aa0000" }, 1000).animate( { backgroundColor : "black" }, 1000);
        }, 100);