views:

464

answers:

3

I've created a custom jQuery function and I want to call it for every match of an element on the page when it loads. The syntax I'm using to call the function:

$(document).ready(function() {
    $("div.class1.class2").fixClassesForIE6("a","b");
});

That syntax does nothing. The fixClassesForIE6 function is never called.

I think I maybe need to use 'trigger' in some form? But I'm not entirely sure.

Ultimately, I want:

  1. the page to load
  2. find all DIV elements on the page with class1 and class2
  3. Trigger my function for each match
A: 

For selecting all DIV elements on the page with class1 and class2, I think that you should use $("div.class1,div.class2") instead.

For doing something for each matching div w/ class1 or class 2, i think that you can use the each() function here?

$("div.class1,div.class2").each(
     function(index){
       //do something here
     });
devpl
I'm guessing DA is looking for elements that have both of those classes, not different elements that have either class.
ScottE
ScottE: agreed. `.class1.class2` is perfectly valid for this.
Roatin Marth
Indeed, I'm looking for BOTH classes. Hence the 'fixIE6' part of the function name. You can't attach CSS using double-class names in IE6, so I'm creating a jQuery script to fix that.
DA
+1  A: 

I can see 2 possible problems:

1) Your selector is wrong. You can confirm this by executing your selector, by itself, in a JavaScript debugger, such as FireBug for Firefox.

2) The method fixClassesForIE6() ... is that your method? I do not recognize it as a JQuery method. If it is your method, then this will not work - because you are calling it on the results of the selector, which don't have that method. Instead, try something like:

$(document).ready(function() { 
    var elements = $("div.class1.class2");
    elements.each(function(){ fixClassesForIE6(this, "a","b"); });
}
Matt
Hmm...sorry, YES, that was my own method. I completely omitted that. Here's where I ended up and this seems to be working: $.fn.fixClassesForIE6 = function(class1, class2){ alert("function called!");};$(document).ready(function() { $("div.class1.class2").fixClassesForIE6("","");}); If I run that, it will pop up an alert for each and every DIV on have on the page with both classes 'class1' and 'class2'.
DA
A: 

Define fixClassesForIE6 using the plugin syntax, and all should be good:

(function($) {
    $.fn.extend({
        fixClassesForIE6: function() {
            return this.each(function() {
                // plugin logic goes here
            })
        }
    })
}(jQuery);

If you've done that and it's not working, then you've got problematic code elsewhere.

Roatin Marth
Well, I'm a dope. I didn't actually post the code I was using to delcare my function. Well, in the interim, I managed to figure things out. I ended up using this syntax:$.fn.$.fn.fixClassesForIE6 = function(myVar1, myVar2){...my logic...};And it seems to work! I assume the plug in syntax might be a bit verbose if one doesn't need it to be a plugin? (That said, for what I'm doing, this might make more sense as a plugin anyways...)
DA