views:

899

answers:

4

I often use links with href='#' when calling ajax resources.

I noticed that IE makes an audible "click" when clicking these links.

Here's a workaround:

$("#element")

.click(function(){return false;})

.bind("click", function(){ alert(this); });

HOWEVER, when I try to encapsulate this functionality in a jQuery plugin, I'm not successfully returning the "clicked" element.

For instance, if I use the approach above I'll get the actual A element that was clicked.

But if I write a plugin like this:

(function($){
    $.fn.clickless = function(fnCallback) {

     return this
      .click(function(){return false;})
      .bind("click", function(){
       fnCallback.call();
      });

    }
})(jQuery);

And then call

$("#element").clickless(function(){
alert(this);
});

I'll get the Window object, which doesn't help when I'm trying to find the actual A tag.

Maybe I'm just writing the plugin incorrectly -- any ideas?

Thanks so much,

Michael

+1  A: 

try this:

(function($){
    $.fn.clickless = function(fnCallback) {

      $.each(this, function() {

        var element = $(this);

        element.click(function(){return false;})
               .bind("click", function(){
                    fnCallback();
               });
     });

     return this;

   }
})(jQuery);
Julian Aubourg
No dice -- same problem.
kaneuniversal
I know this may sound stupid but are you sure your link as its id set to "element" or is it just it's href? If it's the href, use "a[href='#element']" as the selector... or better yet "a[href^='#']" to get all the links in question.
Julian Aubourg
Yes -- each link has a unique ID such as "hlList_43" . . . I was just using "element" as a generic ID in that code.
kaneuniversal
weird, there must be something wrong outside what you've shown because it doesn't make sense. Even the first solution should work.Or... do your ids have to have capital letters and underscores? I heard they could cause problems. Or did you test with a minimal id first? (like "testid")
Julian Aubourg
Yeah; it *is* weird. I just tried a basic (text-only, no underscore) ID and it still didn't work.
kaneuniversal
What version of jQuery? (now that's a desperate question isn't it? ;) )
Julian Aubourg
A: 

Alternatively you'll need to advise your users on how to go into the control panel, sounds, and turn off the sounds for start/end navigation.

I personally hate these click noises so it is the first thing I turn off on a Windows install.

scunliffe
...or get a better browser that doesn't make the clicking sound. But alas, these "answers" don't actually answer the question; [how] can it be disabled using a script?
Ty
A: 

why not just use:

.bind("click", fnCallback);

as that will have this properly set.

Edit: this version actually works:

.bind("click", function(){
    fnCallback.apply(this);
});

poked around inside jQuery's each() to find out what it used

More edit:
on looking at how apply() and call() differ, this may be the more correct way to do it.

.bind("click", function(){
    fnCallback.call(this);
});
cobbal
If you try it, you'll see that that doesn't work.
kaneuniversal
true, actually tried it this time.
cobbal
Well, in that code, "fnCallback" doesn't exist. Also, it doesn't handle the "click" noise. But you're right -- the .apply() function is useful; thank you.
kaneuniversal
Hmm -- any reason why that approach is better than .apply()?
kaneuniversal
syntax for call is call(this, arg0, arg1). syntax for apply is apply(this, arguments), and I was just modifying the bind call in your code, as the rest was good.
cobbal
+1  A: 

Finally got it working --

//jQuery.clickless.js

(function($){    
    $.fn.clickless = function(fnCallback) {        
        return this
                .click(function(){return false;})                
                .bind("click", function(){                        
                    fnCallback.apply(this);                
                });    
           }}
)(jQuery);

Edit:

Thanks go to cobbal and Julian (of the Dharma Initiative) -- much appreciated.

kaneuniversal
Arf, how could I have missed the fact you may have used "this" in the callback! Glad you solved this :)
Julian Aubourg