Is this how you define a function in jQuery?

JavascriptJquery

Javascript Problem Overview


Is this how you define a function in jQuery?

$(document).ready( function () {
    var MyBlah = function($blah) { alert($blah);  };
 });

Now to call the function I do:

MyBlah('hello');

Javascript Solutions


Solution 1 - Javascript

First of all, your code works and that's a valid way of creating a function in JavaScript (jQuery aside), but because you are declaring a function inside another function (an anonymous one in this case) "MyBlah" will not be accessible from the global scope.

Here's an example:

$(document).ready( function () {

    var MyBlah = function($blah) { alert($blah);  };

    MyBlah("Hello this works") // Inside the anonymous function we are cool.

 });

MyBlah("Oops") //This throws a JavaScript error (MyBlah is not a function)

This is (sometimes) a desirable behavior since we do not pollute the global namespace, so if your function does not need to be called from other part of your code, this is the way to go.

Declaring it outside the anonymous function places it in the global namespace, and it's accessible from everywhere.

Lastly, the $ at the beginning of the variable name is not needed, and sometimes used as a jQuery convention when the variable is an instance of the jQuery object itself (not necessarily in this case).

Maybe what you need is creating a jQuery plugin, this is very very easy and useful as well since it will allow you to do something like this:

$('div#message').myBlah("hello")

See also: http://www.re-cycledair.com/creating-jquery-plugins

Solution 2 - Javascript

No, you can just write the function as:

$(document).ready(function() {
    MyBlah("hello");
});

function MyBlah(blah) {
    alert(blah);
}

This calls the function MyBlah on content ready.

Solution 3 - Javascript

No.

You define the functions exactly the same way you would in regular javascript.

//document ready
$(function(){
    myBlah();
})

var myBlah = function(blah){
    alert(blah);
}

Also: There is no need for the $

Solution 4 - Javascript

You can extend jQuery prototype and use your function as a jQuery method.

(function($)
{
    $.fn.MyBlah = function(blah)
    {
        $(this).addClass(blah);
        console.log('blah class added');
    };
})(jQuery);

jQuery(document).ready(function($)
{
    $('#blahElementId').MyBlah('newClass');
});

More info on extending jQuery prototype here: http://api.jquery.com/jquery.fn.extend/

Solution 5 - Javascript

jQuery.fn.extend({
    zigzag: function () {
        var text = $(this).text();
        var zigzagText = '';
        var toggle = true; //lower/uppper toggle
			$.each(text, function(i, nome) {
				zigzagText += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
				toggle = (toggle) ? false : true;
			});
	return zigzagText;
    }
});

Solution 6 - Javascript

The following example show you how to define a function in jQuery. You will see a button “Click here”, when you click on it, we call our function “myFunction()”.

  $(document).ready(function(){
    $.myFunction = function(){ 
      alert('You have successfully defined the function!'); 
    }
    $(".btn").click(function(){
      $.myFunction();
    });
  });

You can see an example here: How to define a function in jQuery?

Solution 7 - Javascript

That is how you define an anonymous function that gets called when the document is ready.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - JavascriptPablo FernandezView Answer on Stackoverflow
Solution 2 - JavascriptgehsekkyView Answer on Stackoverflow
Solution 3 - JavascriptDmitri FarkovView Answer on Stackoverflow
Solution 4 - JavascriptLet'sTalkView Answer on Stackoverflow
Solution 5 - JavascriptSaifView Answer on Stackoverflow
Solution 6 - JavascriptthomasView Answer on Stackoverflow
Solution 7 - JavascriptpupenoView Answer on Stackoverflow