2

Maybe this is simple for some of you but how to add

$.functionName(args)

I've read and tried with jQuery.extend and with $.fn:

$.fn.alertSometing = function(param) {
  alert(param);
};

But when I run

  $('.button').on('click', $.alertSometing('something'));

Returns this error:

Uncaught TypeError: $.alertSometing is not a function(anonymous function)

This is my code: http://jsbin.com/kuxomofoka/edit?html,js,console,output

Can someone explain me how to achive that or explain what I'm doing wrong?

Thanks!

Hiero
  • 2,182
  • 7
  • 28
  • 47
  • 2
    The idea of extending jQuery is that you'll be able to call your own methods (like your `alertSomething`) on an existing jQuery context. For example: `$('.button').alertSomething()`. For a start, the `this` keyword in your function will refer to each element in the context. – haim770 Oct 01 '15 at 07:47
  • but I want to use my $.alertSomething() outside an element, like jquery.cookie does $.cookie('name', 'value'); – Hiero Oct 01 '15 at 07:49
  • What is your expected behavior? what should happen when the button is clicked? – Amit Oct 01 '15 at 07:51
  • @Hiero, Don't use `$.fn` then. Your specific problem here is that you're not passing a function callback to the `on('click')` event. – haim770 Oct 01 '15 at 07:51

3 Answers3

6

$ is just a regular object.

Like all JS objects, unless frozen (it's not) - you can just add properties to it:

$.alertSomething = function(message){ alert(message); };

Similarly for any object:

var obj = {};
obj.alertSomething = function(message){ alert(message); };

In general, if your code does not have anything in particular to do with jQuery, I warmly recommend you do not add it to the jQuery object and instead put it on your own namespace or in a module. This helps avoid naming conflicts. Putting things on $ or $.fn is useful to make plugins.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I want to write this as a plugin, but I dont want to use $myDiv.myPlugin, I just want to use $.myPlugin(something), as jquery.cookie does, so its ok to add proprieties to $? – Hiero Oct 01 '15 at 07:53
  • 2
    @Hiero yes, it is perfectly fine to add properties to $, this is what other plugins do too. – Benjamin Gruenbaum Oct 01 '15 at 07:54
  • Although to be fair, in OP's specific usecase, this fails. If you plug this in directly to OP's code, his code won't call `alert()` on every click, it would call it once. It's more OP problem than yours though. It's worth mentioning. – Madara's Ghost Oct 01 '15 at 08:05
  • @MadaraUchiha this is a community wiki answer, by all means go for it. – Benjamin Gruenbaum Oct 01 '15 at 08:10
-2

Try passing anonymous function as callback. And then use your previously created jquery method.

$('button').on('click', function(event){
    $(this).alertSomething('something');
});
winter
  • 172
  • 6
-2

First , jquery functions(like 'on','click'..) are different with javascript function.

So in your case , should be declare like :

$('.button').alertSometing('something');

QHuy
  • 51
  • 5
  • jQuery functions are absolutely not different from JavaScript functions. jQuery is a JavaScript library, not the other way around. – Madara's Ghost Oct 01 '15 at 08:30
  • jQuery is a JavaScript library , yes you're right . But the jquery function has a little bit different with javascript function . Different in the way using . In javascript how can you override onclick event ? – QHuy Oct 01 '15 at 08:45