Keef Baker Keef Baker - 4 months ago 48
jQuery Question

hasClass() is not a function Jquery

EDIT

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.

/EDIT

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


code:

if(msg.data.match(/^LCERROR/) || msg.data.match(/^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: https://api.jquery.com/hasclass/

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

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

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