3

Hello I am facing this strange JQuery problem. My Js:

$("#flip-counter").centerH();
function centerH() {
var marginL = ( $(window).width() - $(this).width() ) / 2;
$(this).css("marginLeft",marginL+"px");
alert(marginL);
}

Getting this error:

Uncaught TypeError: Object [object Object] has no method 'centerH'         
(anonymous function) 

k jquery.js:2 
l.fireWith jquery.js:2 
p.extend.ready jquery.js:2
D jquery.js:2

jquery.js:2 is empty.

I would understand if none of the code would work but now everything works except that and I can't add any other function. I just keep getting the same error. All files are included properly and when I delete this piece of JS code, my code works like a charm.

Any suggestions?

4 Answers 4

6

You need to extend the jquery,if you want to use the function with jquery objects.

$.fn.centerH = function() {
    var marginL = ( $(window).width() - $(this).width() ) / 2;
    $(this).css("marginLeft",marginL+"px");
    alert(marginL);
}
Sign up to request clarification or add additional context in comments.

Comments

3

This code:

$("#flip-counter").centerH()

...tries to get a property called centerH from the jQuery object returned by $("#flip-counter") and then call it as a function. So there has to be a centerH function on the object.

This code:

function centerH() {
var marginL = ( $(window).width() - $(this).width() ) / 2;
$(this).css("marginLeft",marginL+"px");
alert(marginL);
}

...creates a centerH function but does nothing whatsoever to add it to jQuery.

If you're trying to make a jQuery plug-in, you assign the function to $.fn:

$.fn.centerH = centerH;

Once you've done that, the $("#flip-counter").centerH() line will work. But note that in a jQuery plug-in function, this is not a DOM element (your $(this).css(...) code seems to be treating it as one). Within a jQuery plug-in function, this is the jQuery object on which the function was called (so this.css(...) instead).

2 Comments

I never really used this and done lots of JQuery before, this is not a plugin, just a function in js file which is used for frontend. Surpisingly, this does work like that just somehow doesnt return anything - alaerts 0, but maybe that is my problem, Thank you.
@RokasPetrošius: If you're not doing a plugin, then $("#flip-counter").centerH() is not how you call it, and will give you exactly the error you describe. Of course, $("#flip-counter").click(centerH) or something would be a different story.
1

The centerH() function is in the global scope. But you're calling it from the $("#flip-counter") jQuery object.

If you want to make your own custom functions available to jQuery objects you would create it like this:

jQuery.fn.yourFunction = function() { 
    // do stuff
}

1 Comment

Tried this, the result was the same.
0

You cannot add functions to JQuery like that. You will need to add it as a plugin using jQuery.fn.centerH

More info on:

http://docs.jquery.com/Plugins/Authoring

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.