views:

21

answers:

3

This is the plugin:

(
function($)
{  
    $.fn.foo=function()
    {  
        $(this).focus()

        return $(this)
    }
}
)(jQuery)

Now, $('input#foo').foo() does not get focus. Any idea why?

+1  A: 

Looks like your foo function expects a parameter:

$('input#foo').foo("mymap");
Kris Krause
sorry, i'll remove the parameter. I'm just presenting a minimized view of the function
fabjoa
+1  A: 
(function($){ 

    $.fn.foo= function(map){ 

        return this.each(function(){

           // element-specific code here
           $(this).focus();

        });

    };


})(jQuery);

more about Plugin Authoring.

Reigel
Not working either :(
fabjoa
if your codes is working without errors, that should do... look at this [demo](http://jsfiddle.net/SNmDg/)
Reigel
ok, there must be an error somewhere. i was looking for if something was stealing the focus, but its not the case either. http://jsfiddle.net/LMfrp/
fabjoa
actually this line `if (!("autofocus" in document.createElement("input")))` was not true at all that's why the's no focus happening... [updated demo](http://jsfiddle.net/LMfrp/2/)
Reigel
well, which browser are you using? in firefox 3.6.8, it is true. In Chromium, which enables autofocus, it is not.
fabjoa
So, it worked in firefox then. Yes, not working on Chromium...
Reigel
sorry, its just me being very stupid. I ask the element to get focus before I append it to the DOM in my code, hence err of course. Thanks anyway Reigel!
fabjoa
down-voted for what???
Reigel
i don't know, wasn't me, i've upvoted actually
fabjoa
A: 

Use $(this)[0].focus();

$().focus() fires the events bound to focus for a particular element. $()[0].focus() calls focus on the DOM element itself, which causes the browser focus to change.

Stefan Kendall