Where to Declare Your jQuery Functions

Share this article

qanda
We get asked this question alot so I thought we would clarify it for all those jQuery beginners out there. The questions I will provide answers for:

Questions

  • “This may sound like a really dumb question but where do I declare my jQuery functions?”
  • “Does jQuery code work outside the document ready function?”
  • “Why is my jQuery function is not working?”
  • “Do I put the change/click event in jQuery or HTML element?”

Answers

A simple solution for anyone learning jQuery is to always declare your jQuery code inside the document ready function. This is to ensure that all of the html page elements also known as the DOM (Document Object Model) have loaded before any jQuery code is run. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events as soon as the DOM is ready.
$(document).ready(function() {
  // do stuff when DOM is ready
});
See the Different Types of Document Ready for help with this. These can help avoid conflicts with other jQuery frameworks.

Inside html events

I recommend using jQuery to manage the events on DOM. For one it keeps the html clean and second you can disable the events as easily as attaching them. Another Advantage: Using jQuery to attach events (rather than in the HTML markup) you will never see all those “javascript:void()” messages in the status bar when you hover over a link. For example creating a click event in jQuery:
$(document).ready(function() {
     $("a").click(function() {
         alert("Hello world!");
     });
 });
This is the same as putting it in the html code:
Link

Scope

jQuery functions need to be in scope:
$(document).ready(function() {update();});

function update() { 
    $("#board").append(".");
    setTimeout(update, 1000);
}
or
$(document).ready(function() {update();});

function update() { 
  $("#board").append(".");
  setTimeout('update()', 1000);
}
The second works because the update() within the code block is within scope now.

Frequently Asked Questions about Declaring jQuery Functions

What is the basic syntax for declaring a jQuery function?

The basic syntax for declaring a jQuery function involves using the dollar sign ($) followed by parentheses. Inside the parentheses, you select an HTML element to manipulate. After the parentheses, you use a dot (.) followed by the jQuery method you want to use. Here’s an example:
$(selector).action()
In this example, “selector” is the HTML element you want to manipulate, and “action” is the jQuery method you want to use.

How do I ensure that my jQuery code runs only after the document is fully loaded?

To ensure that your jQuery code only runs after the document is fully loaded, you can use the $(document).ready() method. This method waits until the DOM is fully loaded before executing the jQuery code. Here’s an example:
$(document).ready(function(){
// jQuery methods go here...
});

How can I declare a function in jQuery?

Declaring a function in jQuery is similar to declaring a function in JavaScript. You can use the function keyword followed by the function name and parentheses. Here’s an example:
function myFunction() {
// code to be executed
}

Can I use jQuery without declaring it?

Yes, you can use jQuery without declaring it. However, you need to make sure that the jQuery library is included in your HTML file. You can do this by adding a <script> tag with the source attribute pointing to the jQuery library.

How can I declare a function inside a jQuery method?

You can declare a function inside a jQuery method by passing an anonymous function as an argument to the method. Here’s an example:
$("button").click(function(){
// code to be executed
});
In this example, an anonymous function is declared inside the click() method.

How can I use variables in jQuery?

You can use variables in jQuery just like in JavaScript. You declare a variable using the var keyword, and you can use it inside your jQuery methods. Here’s an example:
var text = "Hello, World!";
$("p").text(text);
In this example, the variable text is used inside the text() method.

How can I declare a global function in jQuery?

You can declare a global function in jQuery by declaring it outside any other function. This makes it accessible from any part of your code. Here’s an example:
function myFunction() {
// code to be executed
}
In this example, myFunction is a global function.

How can I declare a local function in jQuery?

You can declare a local function in jQuery by declaring it inside another function. This makes it accessible only from within that function. Here’s an example:
function outerFunction() {
function innerFunction() {
// code to be executed
}
}
In this example, innerFunction is a local function.

How can I use the this keyword in jQuery?

The this keyword in jQuery refers to the HTML element that triggered the event. You can use it inside your jQuery methods to refer to the current element. Here’s an example:
$("p").click(function(){
$(this).hide();
});
In this example, this refers to the paragraph that was clicked.

How can I use jQuery with other JavaScript libraries?

jQuery uses the dollar sign ($) as a shortcut for jQuery. However, other JavaScript libraries might use the same symbol. To avoid conflicts, you can use jQuery’s noConflict() method. This allows you to create a new shortcut for jQuery, or use the full name instead. Here’s an example:
var jq = $.noConflict();
jq(document).ready(function(){
jq("button").click(function(){
jq("p").text("jQuery is still working!");
});
});
In this example, jq is the new shortcut for jQuery.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week