1

I have a Strange issue with the latest Mozilla Browser 13.0 with Ubuntu. The code works perfectly in other browsers and also previous versions of mozilla.

Here is the Code

$(document).click(function(event) {
  var target = $(event.target);

  if (!target.attr('class').match(/^RefineClick/) && target.parents('.RefineClick').length == 0) {
   jQuery('.RefineClick').fadeOut();
  }
});

And iam getting the following Error : Target.attr('class').... is undefined

Here is the Screenshot for that: http://i47.tinypic.com/dqoits.png

Surjith S M
  • 6,642
  • 2
  • 31
  • 50

3 Answers3

0

Well... working for me?

http://jsfiddle.net/acrashik/b9MCj/

Sergei Zahharenko
  • 1,514
  • 12
  • 16
0

If there is no class attached to the clicked element target.attr('class') returns undefined (since v1.6); in FF that's expressed as the error you see when you attempt to call .match. (In chrome it would be "can't call method match of undefined")

Add a check for an absent class; if (typeof target.attr('class') === "undefined") return;

Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

You are binding that function to a 'click' event on the entire document. Therefore any click would trigger the handler, including those on elements without a class attribute set. While this would work fine if you tested it only on elements with a defined 'class' attribute (in any browser) it would throw the error (in any browser) because you try and call the method .match() on an undefined value.

"As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set." API ref.

Since you are using jQuery, who not use it's full capabilities? It has a built in .hasClass() method:

$(document).click(function(event) {
    var target = $(event.target);

    if (!target.hasClass('RefineClick') && target.parents('.RefineClick').length == 0) {
        $('.RefineClick').fadeOut();
    }
});

Note that the input to that method is the class-name rather than a class selector.

nbrooks
  • 18,126
  • 5
  • 54
  • 66